Advertisement
gitlez

YA: File Upload Processing 20130626091720AAGnj4f

Jun 26th, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2.  
  3. // Yahoo Answers' Question: http://answers.yahoo.com/question/index?qid=20130626091720AAGnj4f
  4.  
  5. /***   Upload Error Messages (Debug and User modes)   ***/
  6. $DebugUploadErrors = array(
  7.     1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
  8.     2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.', // Set by the form's MAX_FILE_SIZE property
  9.     3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
  10.     4 => 'No file was uploaded.',
  11.     6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Internal Operations Error
  12.     7 => 'Failed to write file to disk.', // Internal Server Error
  13.     8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
  14. );
  15. $UserUploadErrors = array(
  16.     1 => 'The selected file exceeds the allowable Server file size. Please select an alternate file.',
  17.     2 => 'The selected file exceeds the allowable Upload file size. Please select an alternate file.',
  18.     3 => 'Upload connection was interuppted or dropped. Please try again.',
  19.     4 => 'Please select a file to upload.',
  20.     6 => 'Internal Server Error (#606). Please inform site Admin.', // Internal Operations Error
  21.     7 => 'Internal Server Error (#707). Please inform site Admin and please try again.', // Internal Server Error
  22.     8 => 'Internal Server Error (#808). Please inform site Admin.' // PHP Extension Library stopped the upload.
  23. );
  24. // Change for whatever is appropriate. You can place this change in if statements checking for admin or debug mode.
  25. $UploadErrors = $DebugUploadErrors; // or $UserUploadErrors
  26.  
  27.  
  28.  
  29. /***   Variables   ***/
  30. $maxFilesize = 2000000;
  31. $allowedExts = Array('gif', 'jpg', 'jpeg', 'mp4', 'png');
  32. $errors = Array(); // Will hold the error messages
  33. $uploadDir = 'useruploads/';
  34.  
  35.  
  36. if( isset($_FILE['file']) ){
  37.     $f = $_FILE['file'];
  38.     $newFile = rtrim($uploadDir, '/') . '/' . $f['name'];
  39.     $ext = pathinfo($f['name'], PATHINFO_EXTENSION);
  40.    
  41.     // Error Checking
  42.     if( !in_array($ext, $allowedExts) ){ // Filetype
  43.         $errors[] = 'Filetype is not supported. Please select a supported file type (' . implode(', ', $allowedExts) . ').';
  44.     }
  45.     if( (int)$f['size'] > $maxFilesize ){ // Filesize
  46.         $errors[] = 'The selected file is too large for this process. Please select a file smaller than ' . $maxFilesize . ' bytes.';
  47.     }
  48.     if( $f['error'] !== UPLOAD_ERROR_OK){ // Upload Error Status (UPLOAD_ERR_OK -> PHP Constant for the value of a successfull upload)
  49.         $errors[] = $UploadErrors[(int)$f['error']];
  50.     }
  51.    
  52.     // If Errors display, otherwise move uploaded file.
  53.     if( isset($errors[0]) ){ // Errors Exist
  54.         echo 'The following errors were found while trying to process your upload: <br />';
  55.         echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul><br />';
  56.     }else if( move_uploaded_file($f['tmp_name'], $newFile)){
  57.         echo 'The file "' . $f['name'] . '" was uploaded successfully.<br />';
  58.     }else{
  59.         echo 'Moving of uploaded file failed. Please check folder and script permissions.<br />';
  60.     }
  61. }else{
  62.     // Normally even if the user doesn't select a file, then the browser still sends the empty variable,
  63.     // and the server sets the upload error to '4' (no file was uploaded). Although some versions of iOS and
  64.     // versions of Safari Browser do not send the empty file upload variable, so the server is unaware there
  65.     // was suppose to be a file upload, so it doesn't populate the $_FILES superglobal, let alone with the
  66.     // error. So this error message can be reached in a couple of different scenarios, but mainly by users
  67.     // who simply browsed to the page, without submitting the form.
  68.     echo 'UPLOAD ERROR: Please select a file to upload.';
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement