PHP is still quite commonly used, and everyone knows about its powerful functions. Here we will implement PHP file upload code analysis. PHP is difficult to implement the upload progress bar because when we upload files to the server, we need to wait until all the files are sent to the server before executing the corresponding PHP file. Before that, the file data is stored in a temporary file, and php cannot obtain the path and size of the file.
Flash supports uploading and downloading files starting with Actionscript 2.0. Although the File Upload progress cannot be obtained on the server side, we can get the file sending progress on the server side. Based on this principle, you can use Flash to make the upload progress bar effect. I have seen some materials on the Internet, but I feel defective. So I studied it myself and strengthened the security and robustness of the program based on the predecessors, and added some customizable parameters. Currently, I know two methods. One is to use the APC extension module written by RasmusLerdorf, founder of PHP, another method is to use the PECL extension module uploadprogress for implementation. Here are two implementation examples for reference. More flexible applications can be modified as needed.
Php uploads a file to determine whether the path to be stored exists is_dir () and if whether the file type meets the requirements of explode ('. ', name) array () in_array (), if the file size meets the requirements $ _ FILES [size], you can upload move_uploaded_file ($ _ FILES [] ['tmp _ name'], the path to store and the file name) save $ _ FILES [] ['name'] or rename: time ().
PHP file upload code:
- Function uploafile ($ path, $Filesize=10241000, $Type=1)
- {
- If (! Empty ($ path )&&! File_exists ($ path ))
- {
- Echo 'the path of the uploaded file does not exist'; exit;
- }
- // File type that can be uploaded
- $Filetype=Array
- (
- 1=>Array ('gif', 'png ', 'jpg', 'jpeg ', 'bmp '),
- 2=>Array ('swf ', 'flv '),
- 3=>Array ('rm ', 'rmvb', 'av', 'wmv ', 'mpg', 'asf', 'mp3', 'wm', 'wmv ', 'Mid '),
- 4=>Array ('txt ', 'Doc', 'xls', 'ppt ', 'pdf', 'xml', 'rar ', 'zip', 'gzip ', 'cab ', 'iso', 'SQL '),
- 6=>Array ('exe ', 'com', 'Scr ', 'bat ')
- );
- // Merge the uploaded file type Arrays
- $ Filetype [5] = array_merge ($ filetype [2], $ filetype [3]);
- // Obtain all keys in the $ _ FILES Array
- $Arr_key=Array_keys($ _ FILES );
- // Determine the name of the file to be uploaded
- $File= $ _ FILES [$ arr_key [0];
- $Exten=Check_file_type($ File ['name']);
- // Determine the File Upload type
- If ($ type! = 7 &&! In_array ($ exten, $ filetype [$ type])
- {
- The echo $ file ['name']. 'file type does not meet the requirements! '; Exit;
- }
- // Determine the file size
- If ($ file ['SIZE']>$ Filesize)
- {
- Echo $ file ['SIZE']. 'cannot exceed'. $ filesize. 'byte'; exit;
- }
- // Start upload
- // Move_uploaded_file ($ file ['tmp _ name'], $ path. $ file ['name']);
- // Re-name the file to avoid Overwriting
- $Newname=Time(). '.'. $ Exten;
- Move_uploaded_file ($ file ['tmp _ name'], $ path. $ newname );
- Return $ newname;
- }
-
- Echo uploafile ('', $Filesize=10241000, $Type=3);
- ?>
The above is the detailed PHP file upload code, it is very simple.