Home  >  Article  >  Backend Development  >  A brief analysis on the method of uploading files in php

A brief analysis on the method of uploading files in php

WBOY
WBOYOriginal
2016-07-25 08:56:50754browse
Copy code

Notice:

tag, when implementing file upload, must be specified as multipart/form-data. In addition, pay attention to the hidden value field of the form option MAX_FILE_SIZE in the file upload.html. You can limit the size of the uploaded file by setting its Value. The value of MAX_FILE_SIZE is just a suggestion for browsers, in fact it can be easily bypassed. The maximum upload file size in PHP settings will not be invalid. However, it is better to add MAX_FILE_SIZE to the form to improve the upload experience.

2, upload.php

  1. $f=&$HTTP_POST_FILES['Myfile'];
  2. $dest_dir='uploads'; //Set the upload directory
  3. $dest=$dest_dir.'/'.date( "ymd")."_".$f['name']; //Set the file name with the date plus the file name to avoid duplication
  4. $r=move_uploaded_file($f['tmp_name'],$dest);
  5. chmod ($dest, 0755); //Set the attributes of the uploaded file
  6. or
copy Code

In the above example, the contents of the $_FILES array are as follows. Assume that the name of the file upload field is userfile (the name can be whatever you want)

$_FILES[’userfile’][’name’] The original name of the client machine file. $_FILES[’userfile’][’type’] The MIME type of the file, which requires the browser to provide support for this information, such as “image/gif”. $_FILES[’userfile’][’size’] The size of the uploaded file, in bytes. $_FILES[’userfile’][’tmp_name’] The temporary file name stored on the server after the file is uploaded. $_FILES[’userfile’][’error’] Error codes related to the file upload Value: 0; No errors occurred and the file was uploaded successfully. Value: 1; The uploaded file exceeds the limit of the upload_max_filesize option in php.ini. Value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. Value: 3; Only part of the file was uploaded. Value: 4; No files were uploaded.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php stream function setNext article:php stream function set