PHP File Upload Question?
-
Ok i want people to be able to upload all these files, but only the images work, what should i add to make this work? Zip, rar, iso, and exe wont work :( ---=== Code ===--- if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/bmp") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "zip") || ($_FILES["file"]["type"] == "rar") || ($_FILES["file"]["type"] == "iso") || ($_FILES["file"]["type"] == "exe"))
-
Answer:
Here's what you do: 1) Validate the extension: First of all, make your code easier. <?php $type = strtolower($_FILES["file"]["type"]); $fileType = null; switch($type) { case 'gif': case 'jpeg': case 'jpg': case 'png': case 'bmp': case 'pjpeg': $fileType = 'image'; break; case 'zip': case 'rar': case 'iso': $fileType = 'archive'; break; case 'exe': $fileType = 'exe'; break; default: exit(); break; } ?> 2) Now you know that the extension is valid. You also know the fileType, so if its not an image then do this: Lets assume that the file has already been uploaded: <?php $file = ... if($fileType!='image') { //show the appropriate header, or set a download file header header("Content-Disposition: attachment; filename=" . urlencode($file)); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header("Content-Length: " . filesize($file)); } ?> this will force the file to be downloaded. 3) If you want to extend your validation, you can always verify that the file is an image: You need to have the EXIF php library included in the PHP build (or just enabled in windows): <?php $imageExtType = exif_imagetype($file); switch($imageExtType) { case IMAGETYPE_GIF: case IMAGETYPE_JPEG: case IMAGETYPE_PNG: case IMAGETYPE_BMP: $isValidImage = true; break; default: return false; break; } I'm not sure about your pjpeg image type, but this should be OK.
Sadasd A at Yahoo! Answers Visit the source
Other answers
just dont restrict the types... it will accept any files.. ^_^ .. I've been through it before... I have an offer to you in different aspect Earn extra money through projects, visit here: http://www.getafreelancer.com/affiliates/lxculango/
exe = application/exe zip = application/zip sry i don't know the MIME type for iso and rar, although theres another way to do this, i just dont remember atm.
Related Q & A:
- How to upload a file to another server using cURL and PHP?Best solution by Stack Overflow
- How to cancel a file upload using an iframe?Best solution by Stack Overflow
- How to use angular-file-upload on a page properly?Best solution by angular-file-upload.appspot.com
- How to use external PHP file in СakePHP 2?Best solution by Stack Overflow
- How to connect php file to ms access?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.