Use PHP to upload files

Source: Internet
Author: User


A common problem on the WebDev site is file upload. In this article, I will explain how to use PHP to implement files
Upload. Design upload table

Our main goal is to upload files from a local computer to the server. To achieve this, we need to create a table
You can select a file and submit it. The following is an example:

<HTML>
<HEAD>
<TITLE> File Upload table </TITLE>
</HEAD>
<BODY>
<TABLE>
<Form enctype = "multipart/form-data" NAME = MyForm
ACTION = submit. php3
METHOD = "POST">
<TR> <TD> Select Upload File </TD> <input name = "MyFile"
TYPE = "File"> </TD> </TR>
<TR> <td colspan = "2"> <input name = "submit" VALUE = "Upload"
TYPE = "submit"> </TD> </TR>
</TABLE>
</BODY>
</HTML>

Note the ENCTYPE = "multipart/form-data" section in the table. This must not be an error; otherwise, the server will not know that you are on
Upload 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 saves it to our specified object.
. 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 "didn't you 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 location.
3. Delete temporary files.

After you press the submit button, the file will be uploaded from your computer to the temporary directory on the server. Files in the temporary directory
The name is a temporary file. You should use the name value of the file field to access it. Here, it is $ MyFile. The real file name uses file
Add "_ name" to the name of the field. $ MyFile_name is used here. Use the copy () function to copy a temporary file $ MyFile
To the specified directory, the copied file name is $ MyFile_name. Do not forget to delete the temporary file after it is completed, or you will have a lot
Unwanted files.

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 about it.
It is impossible. Although W3C can, in fact, neither IE nor Netscape can set the value of the VAUE attribute. It sounds a little cool.
Smile, why can't I set an initial value to make it easier for users to use it? If you do that, you will find that
There is 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 can it prevent me from uploading your/etc/passwd file? Further, you do not need to press the submit button.
Set the value of the file field, and then use a JavaScript program to simulate the commit action... wow... I can process any
File. For this reason, the browser simply ignores the VALUE field of the file field in the <INPUT> mark.

Limit File Size

Another cool feature is to restrict the size of uploaded files. You only need to add a <INPUT> flag:

<Input type = "hidden" name = "MAX_FILE_SIZE" value = "100000">

This will not allow users to upload files larger than kb.

Show File Size

To display the file size, you can use the file field name attribute value and the variable "_ size" to access the file. In our example
$ MyFile_size is used. Therefore, if you want to tell the user the size of the uploaded file, you can do the following:

Echo "You have just uploaded $ MyFile_name ";
Echo "The size of the file is $ MyFile_size ";

Permission

Obviously, you need the write permission for the target directory. If a user uploads a file using a text file, the user name should be
"Bobody ". This user must have the write permission on the target directory. Otherwise, you may get the following information:

Warning: Unable to create '/home/berber/berber.txt ':
Permission denied
In/home/berber/submit. php3 on line 5

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.