PHP File Upload application and principles

Source: Internet
Author: User
Tags form post php file upload

You must know several tips for uploading files in php. One must be the data uploaded by the form post, which is accepted by php move_uploaded_file and saved to the specified path on the server.

1. form label enctype attribute.

2. $ _ FILES system functions. // Convert the uploaded content to an array.

3. move_uploaded_file function. // Move the uploaded files stored in the cache folder to the specified folder.

4. is_uploaded_file function. // Determine whether the object exists.

---------------------------------------

1. form tag

Format: <form enctype = "multipart/form-data" ......>
<Input name = "upfile" type = "file">

2. $ _ FILES system functions
$ _ FILES ['name'] // the original file name uploaded by the client.
$ _ FILES ['type'] // MIME type of the file, for example, "image/gif"
$ _ FILES ['SIZE'] // size of the uploaded file, in bytes.
$ _ FILES ['tmp _ name'] // temporary file name, which is generally the default value.
$ _ FILES ['error'] // upload the relevant code (0: Successful, 1: exceeds the php. ini size. 2: The size exceeds the size specified by the PHP file code. 3: only part of the file is uploaded. 4: no files are uploaded. 5: the size of the uploaded file is 0 .)

3. move_uploaded_file Function
Function for moving the uploaded file to the target location
Move_uploaded_file (temporary file, target location and file name ;)

4. is_uploaded_file Function
Function used to determine the upload MIME type
Is_uploaded_file (MIME );

---------------------------------------

Instance:

The Code is as follows: Copy code
<Form enctype = "multipart/form-data" action = "upload. php" method = "post">
<Input type = "hidden" name = "max_file_size" value = "100000">
<Input name = "userfile" type = "file">
<Input type = "submit" value = "Upload File">
</Form>

Note:

1. enctype = "multipart/form-data" must be specified in the form to let the server know that the file carries regular form information.
2. There must be a form area that can be set to the maximum length of the uploaded file, that is, the maximum size of the file to be uploaded (calculated in bytes). It is a hidden value range, that is, max_file_size, you can set the Value to limit the size of the uploaded file. This prevents users from having to wait for the upload of a large file before finding that the file is too big and troublesome. However, this value can be bypassed by others. For security reasons, it is best to configure the upload_max_filesize option in the php. ini file to set the file upload size. The default value is 2 MB.

The Code is as follows: Copy code

Function uploadfile ($ type, $ name, $ ext, $ size, $ error, $ tmp_name, $ targetname, $ upload_dir)
{
$ MAX_SIZE = 2000000;
$ FILE_MIMES = array ('image/pjpeg ', 'image/jpeg', 'image/jpg ', 'image/gif', 'image/png ');
$ FILE_EXTS = array('.jpg ', '.gif', '.png ','. JPG ','. GIF ','. PNG ');

$ File_path = $ upload_dir. $ targetname;

If (! Is_dir ($ upload_dir ))
{
If (! Mkdir ($ upload_dir ))
Die ("the File Upload directory does not exist and the file upload directory cannot be created ");
If (! Chmod ($ upload_dir, 0755 ))
Die ("the File Upload directory permission cannot be set to readable and writable ");
}

If ($ size> $ MAX_SIZE)
Die ("the size of the uploaded file exceeds the specified size ");

If ($ size = 0)
Die ("select the uploaded file ");

If (! In_array ($ type, $ FILE_MIMES) |! In_array ($ ext, $ FILE_EXTS ))
Die ("please upload the file type that meets the requirements ");

If (! Move_uploaded_file ($ tmp_name, $ file_path ))
Die ("failed to copy the file, please upload it again ");

Switch ($ error)
{
Case 0:
Return;
Case 1:
Die ("the uploaded file exceeds the limit of the upload_max_filesize option in php. ini ");
Case 2:
Die ("the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form ");
Case 3:
Die ("only part of the file is uploaded ");
Case 4:
Die ("no files are uploaded ");
}
}

How to upload multiple files? For example, upload three files simultaneously.

The Code is as follows: Copy code

You only need


<Input name = "userfile" type = "file">

Change

<Input name = "userfile []" type = "file">
<Input name = "userfile []" type = "file">
<Input name = "userfile []" type = "file">

When this function is called, $ _ FILES ['userfile'] ['name'] [0] indicates information about the first file, the same applies to others.

 

Some php performance configurations can be modified if necessary.

Max_execution_time = 30; maximum time (in seconds) for running each PHP page. The default value is 30 seconds.
Max_input_time = 60; maximum time required for receiving data on each PHP page. The default value is 60 seconds.
Memory_limit = 128 m; maximum memory size consumed by each PHP page. The default value is 128 M. If you think it is small, you can set it to bigger. 128 is enough.
Max_execution_time= 600
Max_input_time= 600
Upload_max_filesize = 32 m
Post_max_size = 32 m

If the file size is limited, we can solve the problem as follows:

Open php. ini, first find

File_uploads = on; whether to allow file upload over HTTP. ON is enabled by default.

Upload_tmp_dir; upload the file to the place where the temporary file is stored on the server. If it is not specified, the default Temporary Folder will be used.

Upload_max_filesize = 8 m; wangwen business, that is, the maximum file size allowed to be uploaded. The default value is 2 MB.

Post_max_size = 8 m; the maximum value that can be received by posting a form to PHP, including all values in the form. The default value is 8 Mb.

Generally, after the preceding four parameters are set, uploading the file <= 8 m is not a problem, but the network is normal.

However, if you want to upload a large file larger than 8 Mb, you can only set the above four items.

 
Further configure the following parameters

Max_execution_time = 600; maximum time (in seconds) for running each PHP page. The default value is 30 seconds.

Max_input_time = 600; maximum time required for receiving data on each PHP page. The default value is 60 seconds.

Memory_limit = 8 m; maximum memory consumed by each PHP page. The default value is 8 M.

After modifying the preceding parameters, you can upload a large volume of files as permitted by the network.

Max_execution_time= 600
Max_input_time= 600
Memory_limit = 32 m
File_uploads = on
Upload_tmp_dir =/tmp
Upload_max_filesize = 32 m
Post_max_size = 32 m

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.