PHP Basics (5)-File upload
PHP Basics-File Upload
Sample code Download page http://xieye.iteye.com/blog/1336095
Enables users to upload text and binary files. With PHP's authentication and file manipulation functions, you have full control over which people are allowed to upload and what to do with the file upload.
Related settings: See PHP.ini's File_uploads,upload_max_filesize,upload_tmp_dirpost_max_size and max_input_time settings options
By default, PHP is configured to allow uploads, and the file size cannot exceed 2M.
PHP upload need to build a special form, in fact, the form is more than a property enctype= "Multipart/form-data", there is a form element more than one file element.
The Move_uploaded_file function needs to be used on the server, and there is a $_file Hyper global variable available.
Copy from PHP manual below
<<<
$_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 side, so do not assume this value is assumed.
$_files[' userfile ' [' Size ']
The size of the uploaded file, in bytes.
$_files[' UserFile ' [' Tmp_name ']
Temporary file names stored on the server after the files have been uploaded.
$_files[' userfile ' [' Error ']
The error code associated with the file upload. This item was added in version PHP 4.2.0.
>>>
Recommendation 1: Users uploading files in real-world applications should not be placed in the same directory as the program (which is a bad habit), and this tutorial has been put together for the sake of simplicity.
Recommendation 2: For system security, always should rename the upload file, if you want to save the original file name, you can save to the database.
Recommendation 3: If users often upload files, they should be in a directory under a subdirectory, such as the UploadFile directory has 20120102,20120103, such as the Daily directory,
The files uploaded by the user are stored in their respective directories by day.
Recommendation 4: It is not recommended to store uploaded files in their respective folders, unless there are fewer files. System retrieval slows down because there are too many files in the same directory (for example, more than 10,000)
, whether Linux or Windows.
Recommendation 5: If a subdirectory is built, it is often necessary to store the path in the database.
Example 1:
http://localhost/command/peixun/upload/1.php
Normal upload file code, after uploading can go to the folder to check if it really uploaded.
Example 2
Reference source; http://www.phpletter.com/Our-Projects/AjaxFileUpload/
Using the jquery library and a jquery plugin ajaxfileupload
Test URL
http://localhost/command/peixun/upload/2.php
Using JS to upload the file, the client code has changed, but the service-side code is completely unchanged.
Code can be downloaded
1.php
longs; echo $html; } else { $file = $_files[' file1 '); $oldname = $file [' Tmp_name ']; $result = Move_uploaded_file ($oldname, $file [' name ']);//You can replace the name echo $file [' name '] at will. ' Upload successfully, please go to the current folder to check '; }
2.php
Ajax File Upload Example
longs; echo $html; } else { //Note: This is not displayed. $file = $_files[' filetoupload '); $oldname = $file [' Tmp_name ']; $result = Move_uploaded_file ($oldname, $file [' name ']);//Optionally replace the name echo ' {} '; }