communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "file handling" - download "file upload"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

file handling - file upload was created/last modified on Sat 17 Apr 2004 7:33 AM

an easy file upload script.
specify the filetypes allowed, the max filesize and the directory to upload the files.

<?php

// specify the directory where the uploaded file should end up
$path = 'upload/' ;

// specify the filetypes allowed
$allowed = array('image/gif','image/pjpeg','image/jpeg','image/png');

// specify the max filesize in bytes
$max_size = 200000;

if(isset(
$HTTP_POST_FILES['userfile']))
{
  if(
is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
  {
   if(
$HTTP_POST_FILES['userfile']['size'] < $max_size)
   {
    if(
in_array($HTTP_POST_FILES['userfile']['type'],$allowed))
    {
     if(!
file_exists($path . $HTTP_POST_FILES['userfile']['name']))
     {
      if(@
rename($HTTP_POST_FILES['userfile']['tmp_name'],$path.$HTTP_POST_FILES['userfile']['name']))
      {
       
$html_output = 'Upload sucessful!<br>';
       
$html_output .= 'File Name: '.$HTTP_POST_FILES['userfile']['name'].'<br>';
       
$html_output .= 'File Size: '.$HTTP_POST_FILES['userfile']['size'].' bytes<br>';
       
$html_output .= 'File Type: '.$HTTP_POST_FILES['userfile']['type'].'<br>';
       
$image = $HTTP_POST_FILES['userfile']['name'] ;
      }else{
       
$html_output = 'Upload failed!<br>';
       if(!
is_writeable($path))
       {
        
$html_output = 'The Directory "'.$path.'" must be writeable!<br>';
       }else{
        
$html_output = 'an unknown error ocurred.<br>';      
       }
      }
     }else{
      
$html_output = 'The file already exists<br>';
     }
    }else{
     
$html_output = 'Wrong file type<br>';
    }
   }else{
    
$html_output = 'The file is too big<br>';
   }
  }
}else{
  
$html_output = '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">';
  
$html_output .= '<input type="file" name="userfile">';
  
$html_output .= '<input type="submit" value="upload">';
  
$html_output .= '</form>';
}

echo
'<html><head><title>Uploader</title></head><body>';
echo
$html_output;
echo
'</body></html>';

?>

learn more about some of the PHP functions used in this snippet: array, dir, echo, else, end, file, file_exists, filesize, filetype, for, in_array, is_uploaded_file, is_writeable, isset, max, rename

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari