Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of PHP file upload code_PHP tutorial

Detailed explanation of the usage of PHP file upload code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:421121browse

This article is an article suitable for PHP beginners to tell you how to edit the PHP file upload code. Before editing, we need to understand a few points and understand the FILES global variable. There are friends who need to learn PHP file upload. You can refer to this article.

php file upload code writing process

1. First determine whether to upload the file
2. If there is any error, please check again to see if there is an error in the upload
3. If an error occurs, an error message will be prompted
4. If there are no errors, then determine the file type
5. If the type meets the conditions, then determine whether the file exists in the specified directory
6. If not, move the file to the specified directory

Some things you must know when uploading files in php

$_FILES['myfile']['name'] refers to the name of the uploaded file
$_FILES['myfile']['type'] refers to the type of file being uploaded
$_FILES['myfile']['size'] refers to the size of the uploaded file, in bytes (B)
$_FILES['myfile']['tmp_name'] refers to the name of the temporary copy file of the uploaded file stored in the server. After the file is moved to the specified directory, the temporary file will be automatically destroyed.
$_FILES['myfile']["error"] refers to the status code of errors that may occur during file upload. The meaning of each status will be explained later.


Let’s take a look at the HTML part first.

The code is as follows Copy code
 代码如下 复制代码

?


上传:

?

Upload:

Description:

 代码如下 复制代码


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

The action="upload.php" in the form tag means that when you click submit in this form, the upload command will be sent to the page called upload.php for processing. method="post" refers to sending in post mode. The enctype="multipart/form-data" attribute specifies which content type to use when submitting this form. When the form requires binary data, such as file content, please use "multipart/form-data", this attribute is necessary if you want to upload files. Type="file" in input specifies that the input should be processed as a file, and there will be a browse button behind the input. Let’s look at a PHP processing page upload.php
The code is as follows Copy code
if($_FILES['myfile']['name'] != '') { <🎜> ​if($_FILES['myfile']['error'] > 0) { echo "Error status:" . $_FILES['myfile']['error']; } else {   move_uploaded_file($_FILES['myfile']['tmp_name'] , "uploads/" . $FILES['myfile']['name']); echo "<script>alert(Upload successful!);</script>"; } } else{ echo "<script>alert(Please upload files!);</script>"; } ?>

The above is super simple, let’s upgrade it now


1. upload.php

The code is as follows Copy code
 代码如下 复制代码



 
    ddd
       
      
 
       
   


       
           
           
           
           
       
请填写用户名
请简单介绍文件
请上传你的文件

   

 

ddd                                                                                                                                                                                                                          When uploading files, please note: 1. Enctyp is required, 2. method = " post " -- >
                                                                                        & Lt; TR & gt; & lt; td & gt; please fill in the username & lt;/td & lt; & lt; & lt; input type = "text" name = "username" & lt;/td & gt; & lt;/tr & gt;
Please briefly introduce the file