简易文件上传
function file_list($dir,$pattern=""){
$arr=array();
$dir_handle=opendir($dir);
if($dir_handle){
while(($file=readdir($dir_handle))!==false){
if($file==='.' || $file==='..'){
continue;
}
$tmp=realpath($dir.'/'.$file);
if(is_dir($tmp)){
$retArr=file_list($tmp,$pattern);
if(!empty($retArr)){
$arr[]=$retArr;
}
} else {
if($pattern==="" || preg_match($pattern,$tmp)){
$arr[]=$tmp;
}
}
}
closedir($dir_handle);
}
return $arr;
}
$d_root = $_SERVER['DOCUMENT_ROOT'];
$store_dir = "$d_root/uploads/";// 上传文件的储存位置
if (!is_dir($store_dir)) {
mkdir($store_dir,0777,true);
}
$file_arr = file_list($store_dir);
foreach ($file_arr as $v=>$k) {
$d_root_no = strlen($d_root);
$l = substr($k,$d_root_no);
echo $v.'号文件下载地址为:
'.$_SERVER['SERVER_ADDR'].$l.'';
}
$upload_file=isset($_FILES['upload_file']['tmp_name'])?$_FILES['upload_file']['tmp_name']:'';
$upload_file_name=isset($_FILES['upload_file']['name'])?$_FILES['upload_file']['name']:'';
$upload_file_size=isset($_FILES['upload_file']['size'])?$_FILES['upload_file']['size']:'';
if($upload_file){
$file_size_max = 1000*1000*200;// 200M限制文件上传最大容量(bytes)
if (!is_dir($store_dir)) {
mkdir($store_dir,0777,true);
}
$accept_overwrite = 1;//Whether overwriting the same file is allowed
// Check the file size
if ($upload_file_size > $file_size_max) {
echo "Sorry, your file size is larger than the limit" ;
exit;
}
// Check read and write files
if (file_exists($store_dir . $upload_file_name) && !$accept_overwrite) {
echo "Files with the same file name exist" ;
exit;
}
//Copy the file to the specified directory
if (!move_uploaded_file($upload_file,$store_dir.$upload_file_name)) {
echo "Failed to copy file";
exit;
}
}
if (isset($_FILES['upload_file'])) {
echo "
You uploaded a file:";
echo isset ($_FILES['upload_file']['name'])?$_FILES['upload_file']['name']:'';
echo "
";
//Client machine The original name of the file.
echo "The MIME type of the file is:";
echo isset($_FILES['upload_file']['type'])?$_FILES['upload_file']['type']:' ';
//The MIME type of the file, which requires the browser to provide support for this information, such as "image/gif".
echo "
";
echo "Upload file size:";
echo isset($_FILES['upload_file']['size'])?$_FILES['upload_file ']['size']:'';
//The size of the uploaded file, in bytes.
echo "
";
echo "The file is temporarily stored as:";
echo isset($_FILES['upload_file']['tmp_name'])?$ _FILES['upload_file']['tmp_name']:'';
//The temporary file name stored on the server after the file is uploaded.
$erroe = isset($_FILES['upload_file']['error'])?$_FILES['upload_file']['error']:'';
switch($erroe){
case 0:
echo "Upload successful"; break;
case 1:
echo "The uploaded file exceeds the value limited by the upload_max_filesize option in php.ini."; break;
case 2:
echo "The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form."; break;
case 3:
echo "Only part of the file was uploaded"; break;
case 4:
echo "No files were uploaded"; break;
case 6:
echo "No cache directory"; break;
case 7:
echo "The upload directory is not readable"; break;
case 8:
echo "Upload stopped"; break;
default:
echo "No file uploaded"; break;
}
echo "
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31