Uploading of PHP files

Source: Internet
Author: User

<?phpheader (' Content-type:text/html;charset=utf-8 '), if (Isset ($_post[' submit ')) {//Test form ' submit ' whether there is an upload, If there is an upload to continue the next step, if not then it will not respond if (Is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {//detect if the specified file is uploaded via Http post, if it is to continue to the next step, If not the hint ' there may be an attack! ' $arr =pathinfo ($_files[' myfile ' [' name ']);//output returns the information of the path (what to look for in the manual) $newName =date (' Ymdgis '). Rand (1000,9999); Add 1000 to 9999 random numeric output file name if (Move_uploaded_file ($_files[' myfile '] [' tmp_name '], "upload/{$newName}.{by date $arr [' extension ']}) {//move_uploaded_file moves the file to another file (the function parameter is the path to the file that was written and moved to the destination)//($arr [' extension '] This function determines the file suffix name based on the information of the return path//If the uploaded file is moved to the target directory successfully then the output ' upload is successful! ', otherwise output ' move file failed! '//Why write the file name and suffix at the same time? Because the same file name and suffix are overwritten, so write this judgment.         echo  ' upload success! ';    }else{    echo  ' failed to upload file! ';     }    }else{    exit (' There might be an attack! ');     }}var_dump ($_files);? ><! doctype html>

Step 1: Detect the form ' submit ' whether there is an upload, if there is upload to continue the next step, if not then no response.

Step 2: Detect if the specified file is uploaded via HTTP post, if it is the next step, if it is not prompt ' There may be an attack! ‘。

Step 3:move_uploaded_file Move the file to another file (the function parameter is to write the file to be moved and move to the path to the destination file).

Step 4: To upload files at the same time write the file name and suffix name, because if not write, encountered the same file name and suffix will be overwritten, so write a judgment.

Step 5: For the judge, write a formula that determines the name of the file and the suffix. (See the above procedure for details)

Step 6: Upload the file and test the program.


Before uploading, we need to set the PHP configuration option (php.ini) associated with the upload.

Configuration item Possible value function description

File_uploads on determines whether the PHP script on the server can accept HTTP file uploads.

The Memory_limit 8M Setup script can allocate the maximum amount of memory to prevent runaway scripts from monopolizing the server.

Upload_max_filesize 2M restricts php from processing the maximum value of the uploaded file, which must be less than the post_max_size value.

Post_max_size 8M limits the maximum amount of information that can be accepted through the post method.

Upload_tmp_dir NULL The temporary path where the file is stored, which can be an absolute path. This directory must be writable for the user who owns this server process.


Two. $_files multidimensional arrays: used to store various upload-related information (information displayed after the output) (see the Reference manual)

Three. php File upload processing function: For uploading files for subsequent processing.

Is_uploaded_file () Determines whether the specified file was uploaded via HTTP post

When the Move_uploaded_file () file is uploaded, it is first stored in a temporary directory on the server and can be used to move the uploaded file to a new location.


This article is from the "Technology Heaven" blog, so be sure to keep this source http://jonhson.blog.51cto.com/10559861/1686938

Uploading of PHP files

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.