How to upload file in AngularJS?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.