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:
-
- The correct action address. Background I was written in PHP, a start with an absolute address, has been wrong, originally forgot to add HTTP//
- Method= ' Post '
- 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