Use PHP to upload files

Source: Internet
Author: User
A title often seen on the WebDev site is about file upload. In this article, I will explain how to use PHP to upload files. The important goal of designing and uploading a table is to upload the file from the local computer to the server. To

A title often seen on the WebDev site is about file upload. In this article, I will explain how to use PHP to upload files.

Design Upload table

Our important goal is to complete file upload from the local computer to the server. To do this, we need to create a table. answer the following: Select a file and submit it. The following is an example:



File upload table






Select Upload file




Pay attention to the ENCTYPE = 'multipart/form-data' section in the table. This must not be an error; otherwise, the server will not know that you are uploading files.

Design Upload program

Now we have completed the front-end part. let's take a closer look at how the background receives the file and keeps it under the Directory we specified. Next we will start using PHP. This is the submit. php3 program:

If ($ MyFile! = 'None '){
Copy ($ MyFile, '/home/berber/$ MyFile_name ');
Unlink ($ MyFile );
}
Else {
Echo 'You didn't upload any text ?;
}
?>

Believe it or not, this is the entire process. What we do in the program is:

1. check whether a file has been uploaded to the server and passed If ($ MyFile! = 'None ');
2. copy the file to the specified position.
3. delete temporary files.

After you press the submit button, the file will be uploaded from your computer to the temporary directory of the server. The file name in the temporary directory is a temporary file. You should use the name value of the file field to visit it. here is $ MyFile. Add '_ name' to the name value of the file field in the real file name application. $ MyFile_name is used here. Apply the copy () function to copy the temporary file $ MyFile to the specified directory. the copied file name is $ MyFile_name. Do not forget to delete the temporary files after they are completed. Otherwise, you will have a lot of files you don't want.

Set file name

One thing that may make programmers sleep is to try to change the VALUE attribute VALUE of the file field. Not many people know that it is impossible. Although W3C can, in fact, neither IE nor Netscape agrees to set the value of the VAUE attribute. It seems a bit ridiculous. Why can't I set an initial value to make it easier for users to apply it? If you do that, you will invent a security vulnerability. Assume that you log on to my website and I can change the value of the file field in a table. So may I be prohibited from uploading your/etc/passwd file? Furthermore, I don't need to press the submit button. I can set the file field value first, and then use a JavaScript program to simulate the commit action... wow... I can process any files on your machine. For this reason, the browser simply put The VALUE field of the file field in the tag is neglected.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.