Analysis on methods for uploading files in php
-
Note: To upload a file, you must specify it as multipart/form-data. In addition, the hidden Value range of the form option MAX_FILE_SIZE in the Upload file upload.html can be limited by setting its Value. The value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply bypassed. PHP sets the maximum number of uploaded files, which is not invalid. However, it is best to add MAX_FILE_SIZE to the form to improve the upload experience. 2. upload. php
- $ F = & $ HTTP_POST_FILES ['myfile'];
- $ Dest_dir = 'uploads'; // sets the upload directory.
- $ Dest = $ dest_dir. '/'. date ("ymd"). "_". $ f ['name']; // Set the file name to date and add the file name to avoid duplication.
- $ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
- Chmod ($ dest, 0755); // sets the attributes of the uploaded file
- Or
-
In the preceding example, the content of the $ _ FILES array is as follows. Assume that the name of the file upload field is userfile) $ _ FILES ['userfile'] ['name'] The original name of the client machine file. $ _ FILES ['userfile'] ['type'] indicates the MIME type of the file, which must be supported by the browser, for example, "image/gif ". $ _ FILES ['userfile'] ['size'] size of the uploaded file, in bytes. $ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded. $ _ FILES ['userfile'] ['error'] error code value related to the file upload: 0; no error occurs, and the file is 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 is uploaded. Value: 4; no file is uploaded. |