Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Lets make this easier to read/edit
- // I also broke up the 'br' tags, as Yahoo doesn't like them
- $f = $_FILES['file'];
- $acceptedTypes = Array('image/gif','image/jpeg','image/png','image/jpg');
- $fileSizeLimit = 20; // In Kb
- $fileSizeLimit *= 1024; // Change File Size Limit to bytes
- // As oppose to writting $_FILES['file'] each time, now we just write ($f);
- // in_array($needle, $haystack) return true if the $needle is in the $haystack, false otherwise
- // For Debugging Purposes, here is a function to explain the Upload Error Codes.
- function uploadErrorMsg($e){
- $errors = array(
- 0 => 'There was not error',
- 1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
- 2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
- 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.
- );
- return $errors[$e];
- }
- if ( in_array($f['type'], $acceptedTypes) && $f["size"] < $fileSizeLimit){
- if ($f["error"] > 0){
- echo "Return Code: " . $f["error"] . "<b" . "r>";
- echo uploadErrorMsg($f['error']) . "<b" . "r>";
- } else {
- echo "Upload: " . $f["name"] . "<b" . "r>";
- echo "Type: " . $f["type"] . "<b" . "r>";
- echo "Size: " . ($f["size"] / 1024) . " Kb<b" . "r>";
- echo "Temp file: " . $f["tmp_name"] . "<b" . "r>";
- if (file_exists("upload/" . $f["name"])){
- echo $f["name"] . " already exists. ";
- } else {
- move_uploaded_file($f["tmp_name"], "upload/" . $f["name"]);
- echo "Stored in: " . "upload/" . $f["name"];
- }
- }
- } else {
- echo "Invalid file";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement