Learning PHP and MySQL

Example 11-27 Checking the file type

Example 11-27 checks the file type to make sure it’s either a JPEG or a GIF file.

<?php

if($HTTP_POST_FILES['upload_file']['type']  !=  "image/gif"  AND
$HTTP_POST_FILES['upload_file']['type']  !=  "image/pjpeg"  AND
$HTTP_POST_FILES['upload_file']['type']  !="image/jpeg")  {
   $error  
=  "You  may  only  upload  .gif  or  .jpeg  files";
   
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}  else  {
   
//the  file  is  the  correct  format
}

?>

The following line copies the file from the temporary directory into the uploads direc- tory using the same filename.

copy($HTTP_POST_FILES['upload_file']['tmp_name'],"uploads/".
$HTTP_POST_FILES['upload_file']['name']);

Using unlink, let’s remove the temporary file.

unlink($HTTP_POST_FILES['upload_file']['tmp_name']);

Posted by on 08/19 at 08:46 AM

Next entry: Example 11-28 Processing an uploaded file

Previous entry: Example 11-26 Checking the file size

<< Back to main