PHP is still more commonly used, for its powerful function presumably everyone also know, we here to implement PHP upload file code analysis. PHP is very difficult to implement the upload progress bar is because when we upload files to the server, we have to wait until all the files are sent to the server, the corresponding PHP files are executed. Before that, the file data is stored in a temporary file, and PHP cannot get the path and size of the file.
Starting with ActionScript 2.0, Flash supports file uploads and downloads. Although it is not possible to get the file upload progress on the server, we can get the progress of sending the file on the server. According to this principle, flash can be used to make upload progress bar effect. I have seen some information on the internet, but I feel defective. So I studied it, on the basis of the predecessors to enhance the security and robustness of the program, and added some customizable parameters. There are two ways I know now, one is to use the APC extension module written by the founder of PHP Rasmuslerdorf, and the other is to use the PECL Extension Module uploadprogress to implement the example I have here to give two separate implementations for reference. More flexible applications are modified to suit their needs.
PHP upload file to determine if the path to be stored Is_dir (), if the type of file is required explode ('. ', name) array () In_array (), if the size of the file is required $_files[size] can be uploaded Move_uploaded_file ($_files[][' tmp_name '), the path and file name to be stored) is stored directly with $_files[][' name ') or renamed: Time ().
PHP Upload file code:
- function Uploafile ($path, $filesize=10241000, $type= 1)
- {
- if (!empty ($path) &&!file_exists ($path))
- {
- echo ' path to upload file does not exist '; exit;
- }
- Allowed file types to upload
- $ filetype = Array
- (
- 1 => Array (' gif ', ' png ', ' jpg ', ' jpeg ', ' BMP '),
- 2 => Array (' SWF ', ' flv '),
- 3 => Array (' RM ', ' rmvb ', ' avi ', ' wmv ', ' mpg ', ' asf ', ' MP3 ', ' wma ', ' WMV ', ' mid '),
- 4 => Array (' txt ', ' Doc ', ' xls ', ' ppt ', ' pdf ', ' xml ', ' rar ', ' zip ', ' Gzip ', ' cab ', ' ISO ', ' SQL '),
- 6 => array (' exe ', ' com ', ' scr ', ' bat ')
- );
- Upload file type array merge
- $filetype [5] = Array_merge ($filetype [2], $filetype [3]);
- Get all keys for the $_files array
- $ Arr_key = Array_keys ($_files);
- Determine the name of the file to upload
- $ file = $_files[$arr _key[0]];
- $ Exten = Check_file_type ($file [' name ']);
- Determine file Upload type
- if ($type!=7 &&!in_array ($exten, $filetype [$type]))
- {
- echo $file [' name ']. ' The file type does not meet the requirements! '; Exit
- }
- Determine file size
- if ($file [' Size ']>$filesize)
- {
- echo $file [' size ']. ' The file size cannot exceed '. $filesize. ' bytes '; exit;
- }
- Start uploading
- Move_uploaded_file ($file [' Tmp_name '], $path. $file [' name ']);
- To avoid overwriting, re-goner the file name
- $ 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 upload file code, very simple.
http://www.bkjia.com/PHPjc/446545.html www.bkjia.com true http://www.bkjia.com/PHPjc/446545.html techarticle PHP is still more commonly used, for its powerful function presumably everyone also know, we here to implement PHP upload file code analysis. PHP is difficult to implement the upload progress bar because of the ...