Home  >  Article  >  Backend Development  >  Notes on PHP file upload system_PHP tutorial

Notes on PHP file upload system_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:21923browse

The entire system only uses PHP for coding, no JS, no regular expressions... pure PHP... In fact, I don't know anything else...
The following is the html coding of the original form...
Upload file form

Copy code The code is as follows:



Map Name:




File Path:



< br />




The meaning of enctype="multipart/form-data" in the form is to set the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded, which cannot be used for file upload; only when multipart/form-data is used, the file data can be completely transferred and the following operations are performed. enctype="multipart/form -data" is to upload binary data; the value in the form is passed in a binary stream.
This sentence seems useless... I thought this could limit the upload file size. The upload file size is only available in php .ini or in the script of the file uploaded later... In my program, I directly set upload_max_filesize=6M in php.ini.
The following is the upload program
Code
Copy code The code is as follows:

$uploaddir="./map/";
$typearr=array("rar","zip", "w3m","w3x");
$findstr=array("/","\"," ","<",">"); //Filter names
$mapname =str_replace($findstr,'',($_POST['mapname']));
//Get the extension
function upfiletype($filename){
$arr=explode('.', $filename);
$a=count($arr)-1;
return $arr[$a];
}
//Is the string a pure number?
function isNo( $str){
$intarr=array('1','2','3','4','5','6','7','8','9','0 ','.');
$a=str_replace($intarr,'',$str);
return ($a=="")?1:0;
}
/ /////Control window size///////
function html(){
echo <<
File upload

<script> <br>function ResetSize(){ <br>self.resizeTo(300,300) <br>self.moveTo(300,200) <br>} <br></script>


eof;
}
if (isNo($mapname)){
html();
exit ('Correct map name entered');
}
//Determine whether there is a file uploaded
if(!is_uploaded_file($_FILES['map']['tmp_name'])){
html();
exit("You did not upload the file or The uploaded file is larger than 6M");
}
//Determine whether it is a repeated upload
$clintfilename=str_replace($findstr,'',$_FILES['map']['name']);
if(isset($_COOKIE['upload'])){
foreach($_COOKIE['upload'] as $value){
if($value==$clintfilename){
html ();
exit("Please do not upload again");
}
}
}
$filetype=strtolower(upfiletype($_FILES['map']['name'] ));
if(!in_array($filetype,$typearr)){
html();
exit("Please upload the correct map file, support rar, zip, w3m, w3x");
}
//Determine whether the server file exists
$filename=$mapname.'.'.$filetype;
for($a=1;$fopen=@fopen(($uploaddir. $filename),"r");$a++){
$filename=$mapname."$a".'.'.$filetype;
fclose($fopen);
}
//Start transferring files;
$filepath=$uploaddir.$filename;
if(move_uploaded_file($_FILES['map']['tmp_name'],$filepath)){
//Set one Cookie is used to check if there are repeated uploads
setcookie("upload[]",$clintfilename,time()+60*12);
html();
echo "Upload successful";
$log=fopen("upload.log","a7");
$logtxt=date("Ymd")."|".date("H:i")."|".$_SERVER[" REMOTE_ADDR"]."|".$filename."rn";
fwrite($log,$logtxt);
}
?>

Since this is my first time writing...there are many problems, and I changed them over and over again while writing...I forgot to give you any advice.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320744.htmlTechArticleThe entire system only uses PHP for coding, no JS, no regular expressions... pure PHP... In fact, I don’t know anything else haha... Below is the html code of the original form... Copy the code of the upload file form...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn