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

file upload” makes use of the built-in PHP functions is_uploaded_file( ), in_array( ), file_exists( ), rename( ) and is_writeable( ).

01 <?php
02 
03 // specify the directory where the uploaded file should end up
04 $path = 'upload/' ;
05  
06 // specify the filetypes allowed
07 $allowed = array('image/gif','image/pjpeg','image/jpeg','image/png');
08  
09 // specify the max filesize in bytes
10 $max_size = 200000;
11  
12 if(isset($HTTP_POST_FILES['userfile']))
13 {
14  if(is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name']))
15  {
16   if($HTTP_POST_FILES['userfile']['size'] < $max_size)
17   {
18    if(in_array($HTTP_POST_FILES['userfile']['type'],$allowed))
19    {
20     if(!file_exists($path . $HTTP_POST_FILES['userfile']['name']))
21     {
22      if(@rename($HTTP_POST_FILES['userfile']['tmp_name'],$path.$HTTP_POST_FILES['userfile']['name']))
23      {
24       $html_output = 'Upload sucessful!<br>';
25       $html_output .= 'File Name: '.$HTTP_POST_FILES['userfile']['name'].'<br>';
26       $html_output .= 'File Size: '.$HTTP_POST_FILES['userfile']['size'].' bytes<br>';
27       $html_output .= 'File Type: '.$HTTP_POST_FILES['userfile']['type'].'<br>';
28       $image = $HTTP_POST_FILES['userfile']['name'] ;
29      }else{
30       $html_output = 'Upload failed!<br>';
31       if(!is_writeable($path))
32       {
33        $html_output = 'The Directory "'.$path.'" must be writeable!<br>';
34       }else{
35        $html_output = 'an unknown error ocurred.<br>';      
36       }
37      }
38     }else{
39      $html_output = 'The file already exists<br>';
40     }
41    }else{
42     $html_output = 'Wrong file type<br>';
43    }
44   }else{
45    $html_output = 'The file is too big<br>';
46   }
47  }
48 }else{
49  $html_output = '<form method="post" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'">';
50  $html_output .= '<input type="file" name="userfile">';
51  $html_output .= '<input type="submit" value="upload">';
52  $html_output .= '</form>';
53 }
54  
55 echo '<html><head><title>Uploader</title></head><body>';
56 echo $html_output;
57 echo '</body></html>';
58  
59 ?>