Home  >  Article  >  Backend Development  >  A brief introduction to the principle of php file upload

A brief introduction to the principle of php file upload

怪我咯
怪我咯Original
2017-07-05 10:14:362364browse

PHP file uploadThe principle is simply analyzed. Friends who learn PHP can refer to it. It is indeed much simpler than asp.

//Form uploads can only use the multipart/form-data encoding format
$_FILES system Function ;
$_FILES['myFile']['name'] file Name
$_FILES['myFile']['type']The type of file, restricted by the server
image/**
image/x-png
application/x-zip-compressed
$_FILES['myFile']['size']Upload file size
$_FILES['myFile']['tmp_name']Save the temporary file name after uploading to the service
$_FILES['myFile'][ 'error'] Error code;
0 Success 1 Exceeds the php.ini size 2 Exceeds the value specified by the MAX_FILE_SIZE option
3 Only partially uploaded 5 The uploaded file size is 0

move_uploaded_file(temporary file, target location and file name);
Function to move the file to the target location after uploading
is_uploaded_file(MIME);
Function to determine the uploaded MIME type of the file

The code is as follows:

<form enctyoe="multipart/form-data" method="post" name="upload"> 
<input name="upfile" name="name"> 
</form> 
if(is_uploaded_file($_FILES[&#39;myFile&#39;][&#39;tmp_name&#39;])){ 
$upfile = $_FILES[&#39;upload&#39;]; 
$name = $upfile[&#39;name&#39;]; 
$type = $upfile[&#39;type&#39;]; 
$size = $upfile[&#39;size&#39;]; 
$tmp_name = $upfile[&#39;tmp_name&#39;]; 
$error = $upfile[&#39;error&#39;]; 
switch($type){ 
case &#39;image/pjpeg&#39; : $ok=1; 
break 
} 
if($ok){ 
move_uploaded_file($tmp_name,&#39;up/&#39;.$name); 
}else{ 
echo "文件类型不允许"; 
} 
}


The above is the detailed content of A brief introduction to the principle of php file upload. For more information, please follow other related articles on the PHP Chinese website!

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