note 101460 deleted from features.file-upload by danbrown

From: Date: Fri, 17 Dec 2010 16:03:16 +0000
Subject: note 101460 deleted from features.file-upload by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-175030@lists.php.net to get a copy of this message
Note Submitter: codam ---- Ultimate upload code - part 1 I hope it will be useful to you. It gathers several codes I found in php.net detailled functionalities: -handles most error messages with nice formatting -checks if the file extension is in the $ALLOWED_EXTENSIONS array (in this code: pdf) -removes special characters from the destination file name -if wanted destination file name is already existing, changes it to $fileNamePrefix.++i.$fileNameExtension ////////////////////////////////////////////////////////////////////// //THE FORM ON THE HTML PAGE ////////////////////////////////////////////////////////////////////// <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="userfile" /> <input type="submit" value="Upload" /> </form> ////////////////////////////////////////////////////////////////////// //THE PHP CODE IN upload.php - part 1 ////////////////////////////////////////////////////////////////////// <?php // init constants // Base directory define ("BASE_DIR", "../../"); // Destination directory define ("MEDIA_DIR", "media/"); // Allowed extensions array $ALLOWED_EXTENSIONS = array('pdf'); // Print upload ok message function print_upload_ok($str) { echo '<font color="#009900"><strong>'.$str.'</strong></font>'; } // Print error message function print_error($str) { echo '<font color="#C80000><strong>'.$str.'</strong></font>'; } // Handles upload error message if any function file_upload_error_message($error_code) { switch ($error_code) { case UPLOAD_ERR_INI_SIZE: return 'File exceeds upload_max_filesize value ('.ini_get('upload_max_filesize').') in php.ini or .htaccess'; case UPLOAD_ERR_FORM_SIZE: return 'File exceeds the MAX_FILE_SIZE value ('.display_filesize($_POST['MAX_FILE_SIZE']).') specified in the HTML form'; case UPLOAD_ERR_PARTIAL: return 'File was only partially uploaded'; case UPLOAD_ERR_NO_FILE: return 'No file was uploaded'; case UPLOAD_ERR_NO_TMP_DIR: return 'Missing a temporary folder'; case UPLOAD_ERR_CANT_WRITE: return 'Failed to write file to disk'; case UPLOAD_ERR_EXTENSION: return 'File upload stopped by extension'; default: return 'Unknown upload error'; } } // Checks if extension is valid function isAllowedExtension($fileName) { global $ALLOWED_EXTENSIONS; return in_array(end(explode(".", strtolower($fileName))),$ALLOWED_EXTENSIONS); } // Converts filesize to a more legible format (from bits to 'Byte','KB','MB','GB','TB' or 'PB' depending on the size) function display_filesize($filesize){ if(is_numeric($filesize)){ $decr = 1024; $step = 0; $prefix = array('Byte','KB','MB','GB','TB','PB'); while(($filesize / $decr) > 0.9){ $filesize = $filesize / $decr; $step++; } return round($filesize,2).' '.$prefix[$step]; } else { return 'NaN'; } } // Creates a valid filename by removing special characters function reformat_filename($str) { $allowed = "/[^a-z0-9\\-\\_\\.]/i"; return preg_replace($allowed,"",$str); }

« previous php.notes (#175030) next »