Simple php file Upload (instance) _php instance

Source: Internet
Author: User

Save the following code as uploads.php

Copy Code code as follows:



<! 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 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/";//upload file storage location


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 download address: <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 file upload maximum capacity (bytes)


if (!is_dir ($store _dir)) {


mkdir ($store _dir,0777,true);


}


$accept _overwrite = 1;//whether to allow overwriting of the same file


Check File size


if ($upload _file_size > $file _size_max) {


echo "Sorry, your file capacity is greater than the provisions";


Exit


}


Check read and write files


if (file_exists ($store _dir. $upload _file_name) &&! $accept _overwrite) {


echo "file with same filename";


Exit


}


Copying files to a 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.





The MIME type of the echo 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 that 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 "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 success"; Break


Case 1:


echo "uploaded a file that exceeds the Upload_max_filesize option limit in php.ini." Break


Case 2:


echo "The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form. "; Break


Case 3:


echo "file is only partially uploaded"; Break


Case 4:


echo "No file was uploaded"; Break


Case 6:


echo "does not cache directory"; Break


Case 7:


echo "Upload directory not readable"; Break


Case 8:


echo "Upload stop"; Break


Default:


echo "did not choose to upload file"; Break


}


echo "<script language=javascript>location.replace (location.href);</script>";


}


?>


</body>


</html>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.