Advertisement
gitlez

YA: Upload Processing 20130607135748AAiqlu7

Jun 7th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <?php
  2.  
  3. // In response to: http://answers.yahoo.com/question/index?qid=20130607135748AAiqlu7
  4.  
  5. function uploadErrorMsg($num, $debug=false){
  6.     $msg = 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.',
  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.     return $msg[(int)$num];
  16. }
  17.  
  18. if( isset($_FILES['file']) ){
  19.  
  20.     $f = $_FILES['file'];
  21.    
  22.     if( $f['error'] === UPLOAD_ERR_OK){ // UPLOAD_ERR_OK is a PHP Upload Constant
  23.         $allowedExts = Array("jpg", "jpeg","gif","png");
  24.         $allowedTypes = Array('image/jpeg','image/pjpeg','image/jpg','image/gif','image/png');
  25.         $extension = pathinfo($f['name'], PATHINFO_EXTENSION);
  26.         $newPath = 'upload/' . $f['name'];
  27.        
  28.         if( in_array($f['type'], $allowedTypes) && in_array($extension, $allowedExts)){
  29.  
  30.             echo "Upload: " . $f["name"] . "<br>";
  31.             echo "Type: " . $f["type"] . "<br>";
  32.             echo "Size: " . ($f["size"] / 1024) . " kB<br>";
  33.             echo "Temp file: " . $f["tmp_name"] . "<br>";
  34.            
  35.             if( file_exists($newPath) ){
  36.                 echo $f['name'] . ' already exists.';
  37.             }else if( move_uploaded_file( $f['tmp_name'], $newPath) ){
  38.                 echo $f['name'] . ' uploaded successfully to <b>' . $newPath . '</b>.<br />';
  39.                 echo '<img src="' . $newPath . '" style="max-width: 200px;" />';
  40.             }
  41.         }else{
  42.             echo 'Invalid File';
  43.         }
  44.     }else{
  45.         echo uploadErrorMsg($f['error']);
  46.     }
  47. }else{
  48.     echo 'Nothing To Upload';
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement