Php file upload code usage details. This article is applicable to php beginners. it shows you how to edit the php file upload code. before editing, you need to know a few points and understand the global variables of FILES, you need to learn. This article is an article suitable for php beginners. it tells you how to edit php file upload code. before editing, we need to know a few points and understand the global variables of FILES, if you need to learn how to upload php files, refer to this article.
Php file upload code compilation process
1. first determine whether to upload a file
2. if yes, check whether an error occurs during the upload.
3. if an error occurs, an error message is displayed.
4. if no error is found, determine the file type.
5. if the type meets the condition, check whether the file exists in the specified directory.
6. if not, move the file to the specified directory.
Several things that must be known when uploading files in php
$ _ FILES ['myfile'] ['name'] indicates the name of the uploaded file.
$ _ FILES ['myfile'] ['type'] indicates the type of the file to be uploaded.
$ _ FILES ['myfile'] ['size'] indicates the size of the uploaded file, in bytes (B)
$ _ FILES ['myfile'] ['tmp _ name'] indicates the name of the temporary copy file in the server where the uploaded file exists, after the file is moved to the specified directory, the temporary file will be automatically destroyed.
$ _ FILES ['myfile'] ["error"] indicates the status code of an error that may occur during File upload. the status code will be described later.
Let's take a look at the HTML section.
The code is as follows: |
|
? |
Note:
Action = "upload. php" in form indicates that when you click submit in form, the upload command will be sent to the page called upload. php for processing. Method = "post" refers to the post method. the enctype = "multipart/form-data" attribute specifies the content type to be used when submitting this form, when a form requires binary data, such as file content, use "multipart/form-data". This attribute is required to upload files. When type = "file" in input, it is stipulated that the input should be processed as a file, and there will be a browser button behind the input.
Let's take a look at the upload. PHP processing page.
The code is as follows: |
|
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 (uploaded successfully !); Script "; } } Else { Echo "script" alert (please upload a file !); Script "; } ?>
|
The above is super simple. let's upgrade it now.
1. upload. php
The code is as follows: |
|
Ddd
|
2. uploadProcess. php
The code is as follows: |
|
// Receive $ Username = $ _ POST ['username']; $ Fileintro =$ _ POST ['fileintro']; // Echo $ username. $ fileintro; // Obtain the file information /* Echo" "; print_r($_FILES); echo " "; */ // Obtain the file size $ File_size = $ _ FILES ['myfile'] ['size']; If ($ file_size> 2*1024*1024 ){ Echo" |