本例就一个php,适合学习,本例可以添加到数据库的代码
上传多个文件时候请一次选择多个文件,支持doc jpg pdf等
- <?php
- 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";
- }
- ?>
- <form action="" method="POST" enctype="multipart/form-data">
- <input type="file" name="files[]" multiple/>
- <input type="submit" value="上传"/>
- </form>