javascript,php File Upload Simple implementation

Source: Internet
Author: User
Tags php file upload

Non-Ajax, non-IFRAME, the most primitive use of file control files upload, record process memo.

Achieve the goal of uploading files to a specified location.

The file control for the client with input:

<formAction= ' data.php 'Method= ' Post 'enctype= ' Multipart/form-data '>  <inputtype= ' file 'name= ' file '/>  <inputtype= ' Submit '/></form>

The Name property of the control is intended for use with server-side communication. After selecting the file, we can try to see the properties of the file control (do not press the submit button at this time), at which point the files under the control are some of the information:

Then press the Sumbit button to submit the file, form to correctly submit the following several conditions are not less:

      1. The correct action address. Background I was written in PHP, a start with an absolute address, has been wrong, originally forgot to add HTTP//
      2. Method= ' Post '
      3. Enctype= ' Multipart/form-data '

After the form file is submitted to the background, you can use Chrome-logger to track what the file looks like:

Tmp_name is the temporary location of the uploaded file, and after the form is submitted, the server will save the uploaded file as a temporary file. All we need to do is copy this temporary file to the server target folder, and the file name cannot use the original name, because it may be duplicated under the target folder, so we will generate a unique user_uid, here I simply rand a number, Copying a file uses PHP's built-in copy function (the two parameters of the function are the path + file name form):

include' Chromephp.php '; chromephp::Log($_files[' file ']);//For Debug$user _uid=Rand(0, 50000);$file _pos=Strpos($_files[' File '] [' name '], '. ');$file _name=$user _uid. ‘.‘ .substr($_files[' File '] [' Name '],$file _pos+ 1);$savePath=dirname(__file__) . ‘\\‘ .$file _name;Copy($_files[' File '] [' Tmp_name '],$savePath);

Finally, there is a problem, and so after has been unable to upload 7z, zip and other format files, thought is unable to transmit compressed files, and finally found to be php.ini file setup problems, found in the php.ini file upload_max_filesize attributes, the value can be increased. When using $_files in PHP, if there is an error, you can try to print the error number yourself, refer to error Messages explained

Reference: javascript,php File Upload detailed

javascript,php File Upload Simple implementation

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.