Learning PHP and MySQL

Example 11-28 Processing an uploaded file

<?php
$maxsize
=28480;  //set  the  max  upload  size  in  bytes 
if  (!$HTTP_POST_VARS['submit'])  {
  
//print_r($HTTP_POST_FILES);
 
$error="  ";
//this  will  cause  the  rest  of  the  processing  to  be  skipped
//and  the  upload  form  displays
}
if  (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'])  AND
!isset(
$error))  {
  $error  
=  "<b>You  must  upload  a  file!</b><br><br>";
  
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if  ($HTTP_POST_FILES['upload_file']['size']  >  $maxsize  AND  !isset($error))  {
  $error  
=  "<b>Error,  file  must  be  less  than  $maxsize  bytes.</b><br><br>";
  
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
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"  AND  !isset($error))  {
  $error  
=  "<b>You  may  only  upload  .gif  or  .jpeg  files.</b><br><br>";
  
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if  (!isset($error))  {
copy
($HTTP_POST_FILES['upload_file']['tmp_name'],"uploads/".$HTTP_POST_FILES
[
'upload_file']['name']); 
  
unlink($HTTP_POST_FILES['upload_file']['tmp_name']); 
  print  
"Thank  you  for  your  upload.";
  exit;
}
else
{
  
echo  ("$error");
}
?>

<html>
<head></head>
<body>
<form  action="<?=$PHP_SELF?>"  method="post"  enctype="multipart/form-data"> 
Choose  a  file  to  upload:<br>
<input  type="file"  name="upload_file"  size="80">
<br>
<input  type="submit"  name="submit"  value="submit">
</form>
</body>
</html>

Posted by on 08/19 at 08:49 AM
  1. Yes, you may use $_POST to accomplish the same thing. $_GET can be used to access variables submitted using a GET submission as well.

    Posted by  on  02/15  at  11:54 PM
  2. Page 1 of 1 pages

Next entry: Example 11-29 Executing df and displaying the results

Previous entry: Example 11-27 Checking the file type

<< Back to main