PHP Upload file detailed
Uploading Filesfunction bytwo sectionscomposition,HTML pageand the PHP Processing Section . HTMLpageThe main is to allow users to selectSelect the file that you want to upload,The PHP section lets we can put the fileStorageto the specified directory of the server.
A HTML Part
Upload.html
Upload Demo:
Description:
1.Inputin the labeltype= "File", indicate thatprocessing the input as a file.
2.Enctype Specifies what type of content to use when submitting this form . when the form requires binary data, such as the file content, please use "Multipart/form-data", if you want to upload the file, this property is necessary.
more aboutenctypeSee the HTML
Enctype property of the label
Two PHP Section
upload.php
0) { echo "upload failed"; } else { if (move_uploaded_file ($_files[' img '] [' tmp_name '], $DST _dir.$_files[' img ' [' name '])) { echo "upload succeeded" ; } else { echo "upload Failed";}} } else { echo "please upload file";}
Description:
1. global variable $_file
This array contains all the uploaded file information.
Let's assume that the name of the File Upload field is IMG, as shown in the example above. The
$_files[' img ' [' Name ']
The original name of the file that the client uploaded.
$_files[' img ' [' type ']
of the fileMIMEtype, if the browser provides this information. An example is"Image/gif". But thisMIMEtype inPHPthe end does not check, so do not assume that there is this value. $_files['IMG ' [' Size ']: the size of the uploaded file, in bytes.
$_files[' img ' [' Size ']
The size of the uploaded file, in bytes.
$_files[' img ' [' Tmp_name ']
Temporary file names stored on the server after the files have been uploaded.
$_files[' img ' [' ERROR ']
The error code associated with the file upload.
2. about the error code
$_files[' img ' [' ERROR '] has the following types
UPLOAD_ERR_OK
Its value is0, no error occurred and the file upload was successful.
Upload_err_ini_size
Its value is1, the uploaded file exceeds thephp.iniinThe value of the upload_max_filesize option limit.
upload_err_form_size
Its value is2, the size of the uploaded file exceeds theHTMLform inThe value specified by the max_file_size option.
upload_err_partial
Its value is3, the file is only partially uploaded.
Upload_err_no_file
Its value is4, no files were uploaded.
Upload_err_no_tmp_dir
Its value is6, the Temp folder cannot be found. PHP 4.3.10and thePHP 5.0.3introduced.
Upload_err_cant_write
Its value is7, the file write failed. PHP 5.1.0introduced.
3.move_uploaded_file
When the file is uploaded, it is stored in the default temp directory on the server side (unless the upload_tmp_dir in php.ini is set to a different path) and the filename is random. If the file has not been moved elsewhere and has not been renamed, the file will be deleted at the end of the form request. It is therefore necessary to pass Move_uploaded_filemove temporary files.
Copy by experimentcan also be completedMove_uploaded_filefunction, why useMove_uploaded_fileit? There is a sayingMove_uploaded_filewill do some checking to upload files to preventCopySome of the security vulnerabilities that arise. But the specificCopyWhat problems will it bring? I didn't find that. Have to know the classmate, welcome message.
Anyway, sincePHPgiven a certain function, there must be some truth, so use it first.
Three Security checks
Consider using $_files[' img ' [' Size '] and $_files[' img ' [' type '] to do some security checks on uploaded files, such as limit upload type, upload file size, etc.
Report:
PHP configuration parameters related to file uploads