note 58621 deleted from features.file-upload by felipe

From: Date: Sun, 30 Dec 2007 17:54:20 +0000
Subject: note 58621 deleted from features.file-upload by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134327@lists.php.net to get a copy of this message
Note Submitter: info at giel berkers dot com ---- I had problems when uploading files using Internet Explorer and Firefox. I checked for extension and mime-type to make sure it was a jpeg-file my users uploaded. Firefox did it well, only Internet Explorer failed to accept the file saying it wasn't the correct mime-type. After some testing, I discovered that Firefox reads the mime-type of a Jpeg-image as: image/jpeg While Internet Explorer reads it as: image/pjpeg I hope this helps somebody to avoid an evening debugging. To contribute to php.net, I enclosed my secure upload script for Jpeg files: <?php function storefile($var, $location, $filename=NULL, $maxfilesize=NULL) { $ok = false; // Check file $mime = $_FILES[$var]["type"]; if($mime=="image/jpeg" || $mime=="image/pjpeg") { // Mime type is correct // Check extension $name = $_FILES[$var]["name"]; $array = explode(".", $name); $nr = count($array); $ext = $array[$nr-1]; if($ext=="jpg" || $ext=="jpeg") { $ok = true; } } if(isset($maxfilesize)) { if($_FILES[$var]["size"] > $maxfilesize) { $ok = false; } } if($ok==true) { $tempname = $_FILES[$var]['tmp_name']; if(isset($filename)) { $uploadpath = $location.$filename; } else { $uploadpath = $location.$_FILES[$var]['name']; } if(is_uploaded_file($_FILES[$var]['tmp_name'])) { while(move_uploaded_file($tempname, $uploadpath)) { // Wait for the script to finish its upload } } return true; } else { return false; } } ?> You can use this script as follow: <? if(isset($_FILES['name'])) { if(storefile("name", $_SERVER['DOCUMENT_ROOT']."test/img")) { echo("Upload succeeded!"); } else { echo("Upload failed..."); } } ?> The last 2 parameters 'filename' and 'maxfilesize' are optional. Offcourse, you can change this script with other mime types and extensions to fit your needs.

« previous php.notes (#134327) next »