Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Yahoo Answers' Question: http://answers.yahoo.com/question/index?qid=20130626091720AAGnj4f
- /*** Upload Error Messages (Debug and User modes) ***/
- $DebugUploadErrors = array(
- 1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
- 2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.', // Set by the form's MAX_FILE_SIZE property
- 3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
- 4 => 'No file was uploaded.',
- 6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Internal Operations Error
- 7 => 'Failed to write file to disk.', // Internal Server Error
- 8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
- );
- $UserUploadErrors = array(
- 1 => 'The selected file exceeds the allowable Server file size. Please select an alternate file.',
- 2 => 'The selected file exceeds the allowable Upload file size. Please select an alternate file.',
- 3 => 'Upload connection was interuppted or dropped. Please try again.',
- 4 => 'Please select a file to upload.',
- 6 => 'Internal Server Error (#606). Please inform site Admin.', // Internal Operations Error
- 7 => 'Internal Server Error (#707). Please inform site Admin and please try again.', // Internal Server Error
- 8 => 'Internal Server Error (#808). Please inform site Admin.' // PHP Extension Library stopped the upload.
- );
- // Change for whatever is appropriate. You can place this change in if statements checking for admin or debug mode.
- $UploadErrors = $DebugUploadErrors; // or $UserUploadErrors
- /*** Variables ***/
- $maxFilesize = 2000000;
- $allowedExts = Array('gif', 'jpg', 'jpeg', 'mp4', 'png');
- $errors = Array(); // Will hold the error messages
- $uploadDir = 'useruploads/';
- if( isset($_FILE['file']) ){
- $f = $_FILE['file'];
- $newFile = rtrim($uploadDir, '/') . '/' . $f['name'];
- $ext = pathinfo($f['name'], PATHINFO_EXTENSION);
- // Error Checking
- if( !in_array($ext, $allowedExts) ){ // Filetype
- $errors[] = 'Filetype is not supported. Please select a supported file type (' . implode(', ', $allowedExts) . ').';
- }
- if( (int)$f['size'] > $maxFilesize ){ // Filesize
- $errors[] = 'The selected file is too large for this process. Please select a file smaller than ' . $maxFilesize . ' bytes.';
- }
- if( $f['error'] !== UPLOAD_ERROR_OK){ // Upload Error Status (UPLOAD_ERR_OK -> PHP Constant for the value of a successfull upload)
- $errors[] = $UploadErrors[(int)$f['error']];
- }
- // If Errors display, otherwise move uploaded file.
- if( isset($errors[0]) ){ // Errors Exist
- echo 'The following errors were found while trying to process your upload: <br />';
- echo '<ul><li>' . implode('</li><li>', $errors) . '</li></ul><br />';
- }else if( move_uploaded_file($f['tmp_name'], $newFile)){
- echo 'The file "' . $f['name'] . '" was uploaded successfully.<br />';
- }else{
- echo 'Moving of uploaded file failed. Please check folder and script permissions.<br />';
- }
- }else{
- // Normally even if the user doesn't select a file, then the browser still sends the empty variable,
- // and the server sets the upload error to '4' (no file was uploaded). Although some versions of iOS and
- // versions of Safari Browser do not send the empty file upload variable, so the server is unaware there
- // was suppose to be a file upload, so it doesn't populate the $_FILES superglobal, let alone with the
- // error. So this error message can be reached in a couple of different scenarios, but mainly by users
- // who simply browsed to the page, without submitting the form.
- echo 'UPLOAD ERROR: Please select a file to upload.';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement