PHP File Upload performance-Single file upload

Source: Internet
Author: User
Tags php file upload sapi
PHP File Upload Function--Single file upload
This section mainly introduces the single file upload function of php upload file.

PHP mainly through the Post method to upload files, files uploaded after the server in the temporary directory (can see the PHP configuration file php.ini options Upload_tmp_dir)

Next, let's look at an example:

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

if (Isset ($_post[' submit ')) {
Switch ($_files[' upload_file ' [' Error ']) {
Case Upload_err_ini_size:
echo "File size exceeds server limit";
Break
Case Upload_err_form_size:
echo "File size exceeds browser limit";
Break
Case Upload_err_partial:
echo "Upload only part of the file";
Break
Case Upload_err_no_file:
echo "No files were uploaded";
Break
Case UPLOAD_ERR_NO_TMP_DIR:
echo "Unable to find temp folder";
Break
Case Upload_err_cant_write:
echo "File failed to write";
Break
Case UPLOAD_ERR_OK:
$upload _dir = './'. Iconv ("UTF-8", "gb2312", $_files[' upload_file ' [' name ']);
if (file_exists ($upload _dir)) {
Echo ';
}else{
if (Move_uploaded_file ($_files[' upload_file ' [' tmp_name '], $upload _dir)) {
Echo ';
}else{
Echo ';
}
}
Break
}
}

?>

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


Let's examine the code first:
1. We see that the code contains variables such as $_files[' upload_file ' [' Error '], so what does that mean?
$_files is a global, two-dimensional array that contains all the information for uploading a file. This array has 5 elements, which describe the properties of the uploaded file, respectively:
$_files[' upload_file ' [' name '] name of the uploaded file
$_files[' upload_file ' [' type '] The MIME type of the uploaded file
$_files[' upload_file ' [' Size '] upload file size (in bytes)
$_files[' upload_file ' [' tmp_name '] temporary name of the uploaded file
$_files[' upload_file ' [' Error '] upload status code

2, we see the code contains a name such as upload_err_partial, then what does this mean?
These are the upload status codes:
Upload_err_ini_size file size exceeds the server limit, set the upload_max_filesize in php.ini
Upload_err_form_size file size exceeds browser limit, set max_file_size hide form field parameter
Upload_err_partial file only part of the upload
Upload_err_no_file file not specified in upload form
Upload_err_no_tmp_dir cannot find a temporary file
Upload_err_cant_write File Write Failed
UPLOAD_ERR_OK File Upload Successful
Code Execution Effect:

3. What is the function of the Move_uploaded_file () function in the code?
Since the file is uploaded to the server's temp directory, it needs to be moved to the specified location using Move_uploaded_file ().

Note: If the file upload fails, it is possible that some parameters in the PHP configuration file are not set properly,

For example, in file uploads

; Whether to allow HTTP file uploads.
; Http://php.net/file-uploads
File_uploads = On

; Temporary directory for HTTP uploaded files (would use system default if not
; Specified).
; Http://php.net/upload-tmp-dir
Upload_tmp_dir = "\xampp\tmp"

; Maximum allowed size for uploaded files.
; Http://php.net/upload-max-filesize
Upload_max_filesize = 2M

; Maximum number of files that can is uploaded via a single request
Max_file_uploads = 20

In addition, there are:

; Maximum size of POST data that PHP would accept.
; Its value is 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; Http://php.net/post-max-size
Post_max_size = 8M

; Maximum amount of memory a script may consume (128MB)
; Http://php.net/memory-limit
Memory_limit = 128M

; Maximum execution time of each script, in seconds
; Http://php.net/max-execution-time
; Note:this directive is hardcoded-0 for the CLI SAPI
Max_execution_time = 30

; Maximum amount of time each script may spend parsing request data. It ' s a good
; Idea-to-limit this-time on productions servers-order to eliminate unexpectedly
; Long running scripts.
; Note:this directive is hardcoded to-1 for the CLI SAPI
; Default Value:-1 (Unlimited)
; Development value:60 (seconds)
; Production value:60 (seconds)
; Http://php.net/max-input-time
Max_input_time = 60

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