Simple php file upload (instance)
- 2020-10-23 20:55:36
- OfStack
Save the following code as ES1en.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<meta name="author" content="xyl" />
<title> Easy file upload </title>
</head>
<style type="text/css">
</style>
<body>
<form enctype="multipart/form-data" action="" method="post">
Please select file: <br>
<input name="upload_file" type="file"><br>
<input type="submit" value=" Upload a file ">
</form>
<br />
<br />
<br />
<br />
<?
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/";// The storage location of uploaded files
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.' File no. Download address is : <a class="download_url" style="color:#01BCC8;text-decoration:none;font-size:16px;font-weight:bold;" href="'.$l.'">'.$_SERVER['SERVER_ADDR'].$l.'<a/><br />';
}
$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 Limit the maximum file upload capacity (bytes)
if (!is_dir($store_dir)) {
mkdir($store_dir,0777,true);
}
$accept_overwrite = 1;// Allow to overwrite the same file
// Check file size
if ($upload_file_size > $file_size_max) {
echo " Sorry, your file size is larger than required ";
exit;
}
// Check read and write files
if (file_exists($store_dir . $upload_file_name) && !$accept_overwrite) {
echo " A file with the same file name exists ";
exit;
}
// Copies the file to the specified directory
if (!move_uploaded_file($upload_file,$store_dir.$upload_file_name)) {
echo " Copy file failed ";
exit;
}
}
if (isset($_FILES['upload_file'])) {
echo "<p> You uploaded the file :";
echo isset($_FILES['upload_file']['name'])?$_FILES['upload_file']['name']:'';
echo "<br>";
// The original name of the client machine file.
echo " Of the file MIME A type of :";
echo isset($_FILES['upload_file']['type'])?$_FILES['upload_file']['type']:'';
// Of the file MIME Type, which requires the browser to provide support for this information, such as" image/gif ".
echo "<br>";
echo " Upload file size :";
echo isset($_FILES['upload_file']['size'])?$_FILES['upload_file']['size']:'';
// The size of the uploaded file, in bytes.
echo "<br>";
echo " The file is stored temporarily after uploading :";
echo isset($_FILES['upload_file']['tmp_name'])?$_FILES['upload_file']['tmp_name']:'';
// A temporary file name that is stored on the server after the file has been uploaded.
$erroe = isset($_FILES['upload_file']['error'])?$_FILES['upload_file']['error']:'';
switch($erroe){
case 0:
echo " Uploaded successfully "; break;
case 1:
echo " More files have been uploaded php.ini In the upload_max_filesize Option limit value ."; break;
case 2:
echo " The size of the uploaded file is over HTML In the form MAX_FILE_SIZE The value specified by the. "; break;
case 3:
echo " Only part of the file is uploaded "; break;
case 4:
echo " No files have been uploaded "; break;
case 6:
echo " No cache directory "; break;
case 7:
echo " The upload directory is not readable "; break;
case 8:
echo " Upload to stop "; break;
default :
echo " No option to upload files "; break;
}
echo "<script language=JavaScript>location.replace(location.href);</script>";
}
?>
</body>
</html>