Advertisement
gitlez

YA: Simple File Upload WC

Jun 1st, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. // Lets make this easier to read/edit
  3. // I also broke up the 'br' tags, as Yahoo doesn't like them
  4. $f = $_FILES['file'];
  5. $acceptedTypes = Array('image/gif','image/jpeg','image/png','image/jpg');
  6. $fileSizeLimit = 20; // In Kb
  7. $fileSizeLimit *= 1024; // Change File Size Limit to bytes
  8.  
  9. // As oppose to writting $_FILES['file'] each time, now we just write ($f);
  10. // in_array($needle, $haystack) return true if the $needle is in the $haystack, false otherwise
  11.  
  12. // For Debugging Purposes, here is a function to explain the Upload Error Codes.
  13. function uploadErrorMsg($e){
  14.     $errors = array(
  15.         0 => 'There was not error',
  16.         1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
  17.         2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
  18.         3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
  19.         4 => 'No file was uploaded.',
  20.         6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Internal Operations Error
  21.         7 => 'Failed to write file to disk.', // Internal Server Error
  22.         8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
  23.     );
  24.     return $errors[$e];
  25. }
  26.  
  27.  
  28.  
  29. if ( in_array($f['type'], $acceptedTypes) && $f["size"] < $fileSizeLimit){
  30.     if ($f["error"] > 0){
  31.         echo "Return Code: " . $f["error"] . "<b" . "r>";
  32.         echo uploadErrorMsg($f['error']) . "<b" . "r>";
  33.     } else {
  34.         echo "Upload: " . $f["name"] . "<b" . "r>";
  35.         echo "Type: " . $f["type"] . "<b" . "r>";
  36.         echo "Size: " . ($f["size"] / 1024) . " Kb<b" . "r>";
  37.         echo "Temp file: " . $f["tmp_name"] . "<b" . "r>";
  38.  
  39.         if (file_exists("upload/" . $f["name"])){
  40.             echo $f["name"] . " already exists. ";
  41.         } else {
  42.             move_uploaded_file($f["tmp_name"], "upload/" . $f["name"]);
  43.             echo "Stored in: " . "upload/" . $f["name"];
  44.         }
  45.     }
  46. } else {
  47.     echo "Invalid file";
  48. }
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement