Advertisement
gitlez

YA: Form Upload Processing Basics

Jun 28th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2. /*
  3.     Response Script for question: http://answers.yahoo.com/question/?qid=20110628154908AABkmr6
  4. */
  5.  
  6. $siteLive = false; // Determines which messages to display.
  7. if($siteLive){
  8.     // Upload Errors ** NOTE ** There is no error number 5. It was an old error, removed.
  9.     $uploadErrors = array(
  10.         1 => 'The Selected File, exceeds the maximum file size allowed. Please alter the file, or select an alternate.',
  11.         2 => 'The Selected File, exceeds the maximum file size allowed. Please alter the file, or select an alternate.',
  12.         3 => 'Your upload connection was dis-connected. Please try again.',
  13.         4 => 'Please Select A File To Upload.',
  14.         6 => 'Internal Server Error. Please try again later. If the problem persists, please inform the site administrator.', // Internal Operations Error
  15.         7 => 'Internal Server Error. Please try again. If the problem persists, please inform the site administrator.', // Internal Server Error
  16.         8 => 'Internal Server Error. Please try again. If the problem persists, please inform the site administrator.' // PHP Extension Library stopped the upload.
  17.     );
  18. }else{
  19.     // Upload Errors ** NOTE ** There is no error number 5. It was an old error, removed.
  20.     $uploadErrors = array(
  21.         1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size', // Both the server and form have max file size limitations
  22.         2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
  23.         3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
  24.         4 => 'No file was uploaded.', // No file was selected for upload.
  25.         6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Internal Operations Error
  26.         7 => 'Failed to write file to disk.', // Internal Server Error
  27.         8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
  28.     );
  29. }
  30.  
  31. if($_SERVER['REQUEST_METHOD'] === "POST"){
  32.     $_fup = $_FILES['uploaded'];
  33.     if($_fup['error'] === UPLOAD_ERR_OK){
  34.         $upDir = "uploads";
  35.         if(!is_dir($upDir)){
  36.             mkdir($upDir, 0777, true);
  37.         }
  38.         $target_path = $upDir . DIRECTORY_SEPARATOR . basename($_fup['name']);
  39.         if(move_uploaded_file($_fup['tmp_name'], $target_path)) {
  40.             echo "The file " . basename($_fup['name']) . " has been uploaded";
  41.         } else{
  42.             echo "There was an error uploading the file, please try again!";
  43.         }
  44.     }else{
  45.         echo $uploadErrors[$_fup['error']] . '<br >';
  46.     }
  47. }
  48. ?>
  49. <form id="form1" name="form1" enctype="multipart/form-data" action="" method="POST">
  50. Please choose a file:
  51. <input type="hidden" name="MAX_FILE_SIZE" value="200000" />
  52. <input name="uploaded" type="file" />
  53. <input type="submit" value="Upload" />
  54. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement