note 101459 added to features.file-upload
| From: | codam at osu1 dot php dot net | Date: | Fri, 17 Dec 2010 15:38:50 +0000 |
| Subject: | note 101459 added to features.file-upload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175027@lists.php.net to get a copy of this message | ||
Ultimate upload code - part 2
//////////////////////////////////////////////////////////////////////
//THE PHP CODE IN upload.php - part 2
//////////////////////////////////////////////////////////////////////
// MAIN
// If there is no post_max_size limitation
if (!empty($_FILES)) {
// If file was correctly uploaded
if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
// If file has a valid extension
if(isAllowedExtension($_FILES['userfile']['name'])) {
// Removes special characters from filename
$reformatedFileName = reformat_filename($_FILES['userfile']['name']);
// If destination file already exists, change the destination file name
if(file_exists(BASE_DIR.MEDIA_DIR.$reformatedFileName)) {
// Gets file name extension
$fileNameExtension = end(explode(".", $reformatedFileName));
// Gets file name prefix without dot '.'
$fileNamePrefix =
substr($reformatedFileName,0,
strlen($reformatedFileName)-strlen($fileNameExtension)-1);
$tempFileName = $reformatedFileName;
$i = 0;
// As long as the filename still exists, increment i
while(file_exists(BASE_DIR . MEDIA_DIR . $tempFileName)) {
$tempFileName = $fileNamePrefix . ++$i . '.' . $fileNameExtension;
}
$destinationFileName = $tempFileName;
} else {
$destinationFileName = $reformatedFileName;
}
// Construct destination path with new filename
$path = BASE_DIR.MEDIA_DIR.$destinationFileName;
// If file copy is successful
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $path)){
//Get file size
$theFileSize = display_filesize($_FILES['userfile']['size']);
print_upload_ok('Upload Successful - File Size: '.$theFileSize);
echo '<script language="javascript">
parent.returnValue("' . MEDIA_DIR . $destinationFileName . '")
</script>';
// If file copy is not successful
} else {
print_error('File could not be moved');
}
}
// if file has an invalid extension
else {
//echo "Invalid file type";
print_error('Invalid file type');
}
}
// If file was not correctly uploaded
else {
print_error(file_upload_error_message($_FILES['userfile']['error']));
}
}
// If there is a post_max_size limitation
else {
print_error('File exceeds the post_max_size value ('.
ini_get('post_max_size').') in php.ini or .htaccess');
}
?>
----
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/101459
Del: integrated -- https://master.php.net/note/delete/101459/integrated
Del: useless -- https://master.php.net/note/delete/101459/useless
Del: bad code -- https://master.php.net/note/delete/101459/bad+code
Del: spam -- https://master.php.net/note/delete/101459/spam
Del: non-english -- https://master.php.net/note/delete/101459/non-english
Del: in docs -- https://master.php.net/note/delete/101459/in+docs
Del: other reasons-- https://master.php.net/note/delete/101459
Reject -- https://master.php.net/note/reject/101459
Search -- https://master.php.net/manage/user-notes.php