Php Basics (5)-File upload php Basics-file Upload sample code download page xieye. iteye. comblog1336095 allows users to upload text and binary files. Using PHP authentication and file operation functions, you can fully control which users are allowed to upload files and how to process the files after they are uploaded. Related settings: see file_uploads and upload_max_f php Basics in php. ini (5)-file Upload
Php Basics-file Upload
Sample Code Download page http://xieye.iteye.com/blog/1336095
Allows users to upload text and binary files. Using PHP authentication and file operation functions, you can fully control which users are allowed to upload files and how to process the files after they are uploaded.
Related settings: see file_uploads, upload_max_filesize, upload_tmp_dirpost_max_size, and max_input_time settings in php. ini.
By default, php configuration allows uploading, and the file size cannot exceed 2 MB.
Php uploads a special form. In fact, the form has an additional attribute, enctype = "multipart/form-data", and a file element in the form element.
The move_uploaded_file function must be used on the server, and a $ _ FILE exclusive global variable is available.
The following is the php Manual.
<
$ _ FILES ['userfile'] ['name']
The original name of the client machine file.
$ _ FILES ['userfile'] ['type']
The MIME type of the file, if the browser provides this information. An example is "image/gif ". However, this MIME type is not checked on the PHP end, so do not take it for granted.
$ _ 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']
The error code related to the file upload. This project was added in PHP 4.2.0.
>>>
Suggestion 1: in actual applications, the files uploaded by users should not be placed in the same directory as the program (that is a bad habit). This tutorial is for the sake of simplicity.
Suggestion 2: for system security, you should always rename the uploaded file. to save the original file name, you can save it to the database.
Suggestion 3: If you often upload files, you should create sub-directories under a directory, for example, the uploadfile directory contains a daily directory such as 201020102, 201020103,
Files uploaded by users are stored in their respective directories on a daily basis.
Suggestion 4: it is not recommended to store the uploaded files in their respective folders by content unless there are few files. If there are too many files in the same directory (for example, more than 10000 files), the system retrieval speed slows down.
Whether it is linux or windows.
Recommendation 5: If sub-directories are created, the path is usually stored in the database.
Example 1:
Http: // localhost/command/peixun/upload/1.php
The normal code for uploading files. After uploading, you can go to the folder to check whether the files are actually uploaded.
Example 2
Reference source; http://www.phpletter.com/Our-Projects/AjaxFileUpload/
The jquery library and a jquery plug-in ajaxfileupload are used.
Test url
Http: // localhost/command/peixun/upload/2.php
When you use js to upload files, the client code changes, but the server code remains unchanged.
Code downloadable
1. php
Longs; echo $ html;} else {$ file = $ _ FILES ['file1']; $ oldname = $ file ['tmp _ name']; $ result = move_uploaded_file ($ oldname, $ file ['name']); // you can replace the echo $ file ['name']. 'upload successful, go to the current folder to check ';}
2. php