note 94126 modified in function.move-uploaded-file by danbrown
| From: | danbrown@php.net | Date: | Sun, 18 Oct 2009 16:14:25 +0000 |
| Subject: | note 94126 modified in function.move-uploaded-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161860@lists.php.net to get a copy of this message | ||
I have looked at a lot of the file upload code listed below and other php documentation and have
developed hopefully a robust single file upload routine. I will later update with a multi file
upload. I have modestly tested the code.
<?php
// 5MB maximum file size
$MAXIMUM_FILESIZE = 5 * 1024 * 1024;
// Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
"/^\.(jpg|jpeg|gif|png|doc|docx|txt|rtf|pdf|xls|xlsx|
ppt|pptx){1}$/i";
$dir_base = "/your/file/location/";
$isFile = is_uploaded_file($_FILES['Filedata']['tmp_name']);
if ($isFile) // do we have a file?
{// sanatize file name
// - remove extra spaces/convert to _,
// - remove non 0-9a-Z._- characters,
// - remove leading/trailing spaces
// check if under 5MB,
// check file extension for legal file types
$safe_filename = preg_replace(
array("/\s+/", "/[^-\.\w]+/"),
array("_", ""),
trim($_FILES['Filedata']['name']));
if ($_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE &&
preg_match($rEFileTypes, strrchr($safe_filename, '.')))
{$isMove = move_uploaded_file (
$_FILES['Filedata']['tmp_name'],
$dir_base.$safe_filename);}
}
}
?>
I use $isFile and $isMove later in the code for error recording.
Make sure your system has the appropriate file loading limits. This caused a lot of headeaches
trying to figure out why some files loaded and some did not. In my case I have an .htaccess file
in the root of the web site with:
php_value post_max_size 16M
php_value upload_max_filesize 6M
You may also need to extend the execution time depending upon the amount of data being transferred.
(sorry if spacing of code is a little off. it was hard to make the note editor like the code
style.)
--was--
I have looked at a lot of the file upload code listed below and other php documentation and have
developed hopefully a robust single file upload routine. I will later update with a multi file
upload. Any comments/corrections would be welcomed. I have modestly tested the code.
<?php
// 5MB maximum file size
$MAXIMUM_FILESIZE = 5 * 1024 * 1024;
// Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
"/^\.(jpg|jpeg|gif|png|doc|docx|txt|rtf|pdf|xls|xlsx|
ppt|pptx){1}$/i";
$dir_base = "/your/file/location/";
$isFile = is_uploaded_file($_FILES['Filedata']['tmp_name']);
if ($isFile) // do we have a file?
{// sanatize file name
// - remove extra spaces/convert to _,
// - remove non 0-9a-Z._- characters,
// - remove leading/trailing spaces
// check if under 5MB,
// check file extension for legal file types
$safe_filename = preg_replace(
array("/\s+/", "/[^-\.\w]+/"),
array("_", ""),
trim($_FILES['Filedata']['name']));
if ($_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE &&
preg_match($rEFileTypes, strrchr($safe_filename, '.')))
{$isMove = move_uploaded_file (
$_FILES['Filedata']['tmp_name'],
$dir_base.$safe_filename);}
}
}
?>
I use $isFile and $isMove later in the code for error recording.
Make sure your system has the appropriate file loading limits. This caused a lot of headeaches
trying to figure out why some files loaded and some did not. In my case I have an .htaccess file
in the root of the web site with:
php_value post_max_size 16M
php_value upload_max_filesize 6M
You may also need to extend the execution time depending upon the amount of data being transferred.
(sorry if spacing of code is a little off. it was hard to make the note editor like the code
style.)
http://php.net/manual/en/function.move-uploaded-file.php