note 101460 added to features.file-upload
| From: | codam at osu1 dot php dot net | Date: | Fri, 17 Dec 2010 15:41:54 +0000 |
| Subject: | note 101460 added to features.file-upload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175028@lists.php.net to get a copy of this message | ||
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);
}
----
Server IP: 89.31.146.176
Probable Submitter: 217.195.18.153
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.php
Edit -- https://master.php.net/note/edit/101460
Del: integrated -- https://master.php.net/note/delete/101460/integrated
Del: useless -- https://master.php.net/note/delete/101460/useless
Del: bad code -- https://master.php.net/note/delete/101460/bad+code
Del: spam -- https://master.php.net/note/delete/101460/spam
Del: non-english -- https://master.php.net/note/delete/101460/non-english
Del: in docs -- https://master.php.net/note/delete/101460/in+docs
Del: other reasons-- https://master.php.net/note/delete/101460
Reject -- https://master.php.net/note/reject/101460
Search -- https://master.php.net/manage/user-notes.php