Home  >  Article  >  Backend Development  >  PHP file upload main code analysis_PHP tutorial

PHP file upload main code analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:16:25853browse

php upload file code:

<?<span php   
 </span><span if</span>(<span $_FILES</span>['myfile']['name'] != ''<span ) {   
</span><span if</span>(<span $_FILES</span>['myfile']['error'] > 0<span ) {   
</span><span echo</span> "错误状态:" . <span $_FILES</span>['myfile']['error'<span ];   
} </span><span else</span><span  {   
</span><span move_uploaded_file</span>(<span $_FILES</span>['myfile']['tmp_name'] , "uploads/" . <span $FILES</span>['myfile']['name'<span ]);   
</span><span echo</span> "<script>alert(上传成功!);</script>"<span ;   
}   www.jbxue.com
} </span><span else</span><span {   
</span><span echo</span> "<script>alert(请上传文件!);</script>"<span ;   
}   
</span>?> 

Before explaining this code, we need to understand the following knowledge.





Understanding this, let’s take a look at the code of upload.php.

First of all, myfile in $_FILES['myfile']['name'] refers to the name value of the file tag uploaded in the above HTML page. Based on this, we can know which input submitted the file we are processing. of.

Then let’s check whether $_FILES['myfile']['name'] is empty. Based on this, we can know whether the user has uploaded files and perform different operations.

If a file is uploaded and the status is 0, it means the upload is successful. We can use the move_uploaded_file method to store the uploaded file in the specified directory. The above example refers to moving the uploaded file to the uploads folder in the same directory. , this path is relative to the directory relative to this PHP file (i.e. upload.php).

For example, if we want to move the uploaded file to a folder called user on the upper level of upload.php, we can write like this: move_uploaded_file ($_FILES['myfile']['tmp_name'], ". /user/". $FILES['myfile']['name']), this method is very convenient and flexible to use. In this way, a file is uploaded to the server, and the directory in the server can be opened to view the file.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440104.htmlTechArticlephp upload file code: ? ( ['myfile']['name'] != '' ( [' myfile']['error'] 0 "Error status:" . ['myfile']['error' ( ['myfile']['tmp_name'] , "uploads/" . ['myfile']['name' "scrip...
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