本例就一個php,適合學習,本例可以添加到數據庫的代碼
上傳多個文件時候請一次選擇多個文件,支持doc jpg pdf等
本站裡面比較實用的多文件上傳有很多,這邊推薦一個《jQuery ajax 無刷新多圖片上傳並記錄到數據庫》
演示
PHP Code
-
- if(isset($_FILES['files'])){
- $res = upload_multiple_file($_FILES['files'],"../upload");
- echo $res;
- }
-
-
- function upload_multiple_file($file,$file_dir="../upload") {
-
- $overwrite=0;
- $allowed_file_type= array("pdf","ppt","pptx","xls","xlxs","doc","docx","jpg", "jpeg", "png", "gif");
- $max_file_size = 2097152;
-
- foreach($_FILES['files']['name'] as $fkey=> $fname){
-
- $ext = pathinfo($fname, PATHINFO_EXTENSION);
- if (!in_array($ext, $allowed_file_type)) {
-
- return "unsupported file format";
- break;
- }
-
-
- }
-
- foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
-
-
- $file_name = $_FILES['files']['name'][$key];
-
-
- $file_size =$_FILES['files']['size'][$key];
-
-
- $file_tmp_name =$_FILES['files']['tmp_name'][$key];
-
-
- $file_type=$_FILES['files']['type'][$key];
-
-
- if($file_size >0) {
- if($file_size > $max_file_size){
-
- $fsize=$max_file_size/1048576;
- return 'File size must be less than '.$fsize.' MB';
- break;
-
- }
- }
-
-
- if(is_dir($file_dir)==false){
-
- $status = mkdir("$file_dir", 0700);
-
- if($status < 1){
-
- return "unable to create diractory $file_dir ";
-
- }
-
- }
-
- if(is_dir($file_dir)){
-
- if($overwrite < 1){
-
- move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);
-
- }
-
- }
-
- // $file_upload_query="INSERT into user_uploads (`u_id`,`file_name`,`file_type`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
- //mysql_query($file_upload_query);
-
-
- }
-
- return "Success";
-
- }
-
- ?>
-
原文地址:http://www.freejs.net/article_biaodan_103.html