note 87787 added to function.move-uploaded-file

From: Date: Sun, 21 Dec 2008 22:13:12 +0000
Subject: note 87787 added to function.move-uploaded-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-148433@lists.php.net to get a copy of this message
I've been having alot of problems validating files on upload. The main problem I was having was matching up the type of file uploaded with the allowed file types, as when you use $_FILES['userfile']['type'], the variable contains the MIME type, which can change consistently from browser to browser. A better method for simply finding the file type is to get the extension of the file by the file name. Here is an example of getting a file type and matching it to an allowed file type (then uploading it and doing some other validation): <?php // Declare variables // Get the basic file information $userfile = $_FILES['userfile']['name']; $file_size = $_FILES['userfile']['size']; $file_temp = $_FILES['userfile']['tmp_name']; $file_err = $_FILES['userfile']['error']; $path = 'images/'; // Create a new file name // This is so if other files on the server have the same name, it will be renamed $randomizer = rand(0000, 9999); $file_name = $randomizer.$userfile; // Get the file type // Using $_FILES to get the file type is sometimes inaccurate, so we are going // to get the extension ourselves from the name of the file // This also eliminates having to worry about the MIME type $file_type = $userfile; $file_type_length = strlen($file_type) - 3; $file_type = substr($file_type, $file_type_length); if(!empty($userfile)) { echo '<div style="font-weight: bold; padding: 6px;">File Uploaded Information</div> <ul> <li>Original File Name: ' .$userfile. '</li> <li>New File Name: ' .$file_name. '</li> <li>File Type: ' .$file_type.'</li> <li>File Size: ' .$file_size. '</li> <li>File Temporary Name: ' .$file_temp. '</li> <li>Fille Error: ' . $file_err. '</li> </ul>'; // limit the size of the file to 200KB if($file_size > 25600) { echo 'FILE SIZE TO LARGE<BR />'; exit(); } // Set allowed file types // Set case of all letters to lower case $file_type = strtolower($file_type); $files = array(); $files[] = 'jpeg'; $files[] = 'jpg'; $files[] = 'gif'; $files[] = 'png'; // Search the array for the allowed file type $key = array_search($file_type, $files); if($key) { echo '<b>File allowed!</b><br />'; } else { echo '<b>ILLEGAL FILE TYPE</b><br />'; exit(); } // Check for errors and upload the file $error_count = count($file_error); if($error_count > 0) { for($i = 0; $i <= $error_count; ++$i) { echo $_FILES['userfile']['error'][$i]; } } else { if(move_uploaded_file($file_temp, 'images/' .$file_name.'')) { echo '<h3>Upload Successful!</h3>'; } else { echo '<h3>ERROR</h3>'; } } } else { echo '<h3>No file has been selected.</h3>'; } ?> In this script, I am looking for image types JPG, PNG, and GIF. You could easily add other types of files ex. $files[] = "docx"; $files[] = "txt"; I am running into some trouble getting PNG's to upload properly, but this is the best method for validating the file type I believe. ---- Server IP: 209.41.74.194 Probable Submitter: 67.163.248.210 ---- Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php Edit -- https://master.php.net/note/edit/87787 Del: integrated -- https://master.php.net/note/delete/87787/integrated Del: useless -- https://master.php.net/note/delete/87787/useless Del: bad code -- https://master.php.net/note/delete/87787/bad+code Del: spam -- https://master.php.net/note/delete/87787/spam Del: non-english -- https://master.php.net/note/delete/87787/non-english Del: in docs -- https://master.php.net/note/delete/87787/in+docs Del: other reasons-- https://master.php.net/note/delete/87787 Reject -- https://master.php.net/note/reject/87787 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148433) next »