PHP file upload application and principles of the detailed _php tutorial

Source: Internet
Author: User
PHP Upload files must know a few tricks, a must be the form post past data, and then by the PHP move_uploaded_file to accept, and then save to the server to specify the path.

1.form Label Enctype property.

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

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

The 4.is_uploaded_file function. Determine if there is any.

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

1.form Label

Format:

Attention

1. Enctype= "Multipart/form-data" in the form must be specified to let the server know that the file has regular form information.
2, must have an upload file maximum length of the form area, which is allowed to upload the maximum value of the file (in bytes), it is a hidden range, that is, max_file_size, by setting its value (value) can limit the size of the uploaded file, Avoid the hassle of having to wait for a large file to be uploaded before the user discovers that the file is too big. But generally others can bypass this value, so security, it is best to configure the Upload_max_filesize option in the php.ini file, set the file upload size, 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 writable");
}

if ($size > $MAX _size)
Die ("The size of the uploaded file 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, please re-upload");

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 uploaded file exceeds the value specified by the Max_file_size option in the HTML form");
Case 3:
Die ("files are only partially uploaded");
Case 4:
Die ("No files are uploaded");
}
}

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

The code is as follows Copy Code

Just add


Change into



Corresponding when calling this function, $_files[' userfile ' [' Name '][0], which represents the relevant file information for the first file, and so on, and so on.

Some about PHP performance configuration, we can also modify when necessary

Max_execution_time = 30; maximum time value per php page (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 eaten by each PHP page, the default 128M. If you feel small, you can set a larger point. 128 enough.
Max_execution_time = 600
Max_input_time = 600
Upload_max_filesize = 32m
Post_max_size = 32m

If the file size is limited, we can resolve the following

Open PHP.ini, first find

File_uploads = on; switch to allow uploading of files over HTTP. The default is on, which is open

Upload_tmp_dir; files uploaded to the server where temporary files are stored, if not specified, will use the system default temporary folder

Upload_max_filesize = 8m; Hope business, that is, the maximum size allowed to upload files. Default is 2M

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

Generally, after setting the above four parameters, uploading <=8m files is not a problem, in the network normal situation.

However, if you want to upload a large volume of >8m files, only set the above four items will certainly be able to pass.


Further configure the following parameters

Max_execution_time = 600; maximum time value per php page (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; max memory consumed per PHP page, default 8M

After modifying the above parameters, it is possible to upload large volume files under normal conditions allowed by the network.

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

http://www.bkjia.com/PHPjc/632639.html www.bkjia.com true http://www.bkjia.com/PHPjc/632639.html techarticle php upload files must know a few tricks, a must be the form post past data, and then by the PHP move_uploaded_file to accept, and then save to the server to specify the path. ...

  • 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.