Learning PHP and MySQL

Saturday, August 19, 2006

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 krautgrrl on 08/19 at 08:49 AM
Chapter 11 Code • (26) Comments • (2) TrackbacksPermalink

Statistics

This page has been viewed 407319 times
Page rendered in 5.9884 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 10
Total anonymous users: 0
Most Recent Visitor on: 05/19/2012 06:03 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine