PHP File upload application and principle of detailed

Source: Internet
Author: User
Tags file size file upload form post html form ini php file php file upload

1.form Label Enctype property.

2.$_files system functions. Converts the uploaded content to an array.

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

4.is_uploaded_file function. Judge whether there is.

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

1.form Label

Format: <form enctype= "Multipart/form-data" ... ...>
<input name= "upfile" type= "File" >

2.$_files system functions
$_files[' name '///client upload file original filename.
$_files[' type ']//file MIME type, such as: "Image/gif"
$_files[' size ']//upload file size, byte units.
$_files[' tmp_name ']//temporary file name, generally default.
$_files[' ERROR '//upload relevant code (0: Success, 1: Over php.ini set size. 2: More than the PHP file code specified size. 3: The file is only partially uploaded. 4: No files are uploaded. 5: Upload file size is 0. )

3.move_uploaded_file function
A function to move a file to a target location after uploading
Move_uploaded_file (temporary file, target location and filename;)

4.is_uploaded_file function
To determine file functions for uploading MIME types
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>

Attention

1. The enctype= "Multipart/form-data" in the form must be specified so that the server knows that the file has regular form information.
2, must have a file can be set to upload the maximum length of the form area, that is, the maximum allowable upload file (in bytes), it is a hidden value range, that is max_file_size, by setting its value (values) can limit the size of the upload file, Avoid the trouble that a user spends time waiting to upload a large file before discovering the file is too big. But generally others can bypass this value, so for security reasons, it is best to configure the Upload_max_filesize option in the php.ini file, set the file upload size, the default is 2M

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 ("File upload directory does not exist and cannot create file upload directory");
if (!chmod ($upload _dir,0755))
Die ("File upload directory permissions cannot be set to readable or writable");
}

if ($size > $MAX _size)
Die ("uploaded file size exceeds the specified size");

if ($size = = 0)
Die ("Please select File to upload");

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 ("Copy file failed, upload again");

Switch ($error)
{
Case 0:
return;
Case 1:
Die ("The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini");
Case 2:
Die ("The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form");
Case 3:
Die ("file is only partially uploaded");
Case 4:
Die ("No file uploaded");
}
}

How do I upload multiple files? Like uploading 3 files at a time.

The code is as follows Copy Code

Simply Place


<input name= "UserFile" type= "File" >

Change into

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

When this function is called, the $_files[' userfile ' [' name '][0] represents the related file information for the first file, and so on.

Some of the PHP performance configuration, if necessary, we can also modify the

Max_execution_time = 30, maximum time for each PHP page to run (seconds), default 30 seconds
Max_input_time = 60: The maximum time required for each PHP page to receive data, default 60 seconds
Memory_limit = 128m; The maximum memory consumed by each PHP page, the default 128M. If you feel small, you can set a larger point. 128 is enough.
Max_execution_time = 600
Max_input_time = 600
Upload_max_filesize = 32m
Post_max_size = 32m

If the file size limit we can solve the following

Open PHP.ini, first find

File_uploads = on; whether to allow the switch to upload files over HTTP. The default is on, which is open

Upload_tmp_dir files are uploaded to the server where temporary files are stored, and the system default temporary folder is used if unspecified

Upload_max_filesize = 8m business, that is, the maximum size of the upload file allowed. The default is 2M

Post_max_size = 8m refers to the maximum value that can be received by the form Post to PHP, including all the values in the form. The default is 8M

Generally, set the above four parameters, upload <=8m file is not a problem, in the normal network situation.

However, if you want to upload >8m files, only set up the above four will be able to do.


Further configure the following parameters

Max_execution_time = 600, maximum time for each PHP page to run (seconds), default 30 seconds

Max_input_time = 600: The maximum time required for each PHP page to receive data, default 60 seconds

Memory_limit = 8m, maximum memory consumed by each PHP page, default 8M

After the above parameters are modified, the network allows the normal situation, you can upload large volume files

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

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.