View Single Post
Old 02-07-2007, 05:50 AM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
with your code example above, you're not uploading any text data so there would really be no need to use a database. you could simply read the file system.

let's say you wanted to store a caption or other info associated with the image. create a database table with 2 fields, filename and caption.

in your form, add a text field for caption.

let's put it all together. (djm's validation suggestions, your upload logic, and store the record in the datbase with a caption)
PHP Code:
<?php
$errors 
= array();

// error checking
if (empty($_FILES['file'])) {
    
$errors[] = 'no file uploaded';
}
if (!
is_uploaded_file($_FILES['file']['tmp_name'])) {
    
$errors[] = 'Possible file upload attack';
}

switch (
$_FILES['file']['error'])
{
    case 
UPLOAD_ERR_INI_SIZE:
        
$errors[] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'

    case 
UPLOAD_ERR_FORM_SIZE:
        
$errors[] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';

    case 
UPLOAD_ERR_PARTIAL:
        
$errors[] = 'The uploaded file was only partially uploaded.';

    case 
UPLOAD_ERR_NO_FILE:
        
$errors[] = 'No file was uploaded.';

    case 
UPLOAD_ERR_NO_TMP_DIR:
        
$errors[] = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
 
    case 
UPLOAD_ERR_CANT_WRITE:
        
$errors[] = 'Failed to write file to disk. Introduced in PHP 5.1.0.';

    case 
UPLOAD_ERR_EXTENSION:
        
$errors[] = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
}

// your error checking
if ($_FILES["file"]["type"] != "image/gif" || $_FILES["file"]["type"] != "image/jpeg")) {
    
$errors[] = 'Wrong image format.';
}

if (
$_FILES["file"]["size"] > 20000) {
    
$errors[] = 'File is too large.';
}

if (
$count($errors)==0) {
    
    echo 
"Upload: " $_FILES["file"]["name"] . "<br />";
    echo 
"Type: " $_FILES["file"]["type"] . "<br />";
    echo 
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo 
"Temp file: " $_FILES["file"]["tmp_name"] . "<br />";

    if (
file_exists("upload/" $_FILES["file"]["name"])) {
        
      echo 
$_FILES["file"]["name"] . " already exists. ";
      
    } else {
        
      
// add record to database
      
$result mysql_query("insert into images (filename, caption) values('".$_FILES["file"]["name"]."',mysql_real_escape_string($_POST['caption'])");
      
      
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" $_FILES["file"]["name"]);
      echo 
"Stored in: " "upload/" $_FILES["file"]["name"];
    }
    
} else {

  echo 
"<b>Errors</b><br />";
  
  foreach (
$errors as $each) {
      echo 
$each."<br />";
  }
}
?>
now to display the images.. just read the image table you are writing to and generate the html.
PHP Code:
<?php
// query db for image filenames and captions
$result mysql_query("select * from images");
while (
$row mysql_fetch_assoc($result)) {
    
// print an HTML image tag
    
echo "<img src='upload/".$row['filename']."' alt='".htmlentities($row['caption'])."' title='".htmlentities($row['caption'])."'>";
    
// print the caption and 2 line breaks
    
echo "<br />".$row['caption']."<br /><br />";
}
?>
__________________
Mike
sde is offline   Reply With Quote