Home > php教程 > php手册 > 纯php多文件上传

纯php多文件上传

WBOY
Release: 2016-06-13 10:19:09
Original
1211 people have browsed it

本例就一个php,适合学习,本例可以添加到数据库的代码

上传多个文件时候请一次选择多个文件,支持doc jpg pdf等

本站里面比较实用的多文件上传有很多,这边推荐一个《jQuery ajax 无刷新多图片上传并记录到数据库》

演示

PHP Code
  1. if(isset($_FILES['files'])){
  2. $res = upload_multiple_file($_FILES['files'],"../upload");
  3. echo $res;
  4. }
  5. function upload_multiple_file($file,$file_dir="../upload") {
  6. $overwrite=0;
  7. $allowed_file_type= array("pdf","ppt","pptx","xls","xlxs","doc","docx","jpg", "jpeg", "png", "gif");
  8. $max_file_size = 2097152;
  9. foreach($_FILES['files']['name'] as $fkey=> $fname){
  10. $ext = pathinfo($fname, PATHINFO_EXTENSION);
  11. if (!in_array($ext, $allowed_file_type)) {
  12. return "unsupported file format";
  13. break;
  14. }
  15. }
  16. foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
  17. $file_name = $_FILES['files']['name'][$key];
  18. $file_size =$_FILES['files']['size'][$key];
  19. $file_tmp_name =$_FILES['files']['tmp_name'][$key];
  20. $file_type=$_FILES['files']['type'][$key];
  21. if($file_size >0) {
  22. if($file_size > $max_file_size){
  23. $fsize=$max_file_size/1048576;
  24. return 'File size must be less than '.$fsize.' MB';
  25. break;
  26. }
  27. }
  28. if(is_dir($file_dir)==false){
  29. $status = mkdir("$file_dir", 0700);
  30. if($status
  31. return "unable to create diractory $file_dir ";
  32. }
  33. }
  34. if(is_dir($file_dir)){
  35. if($overwrite
  36. move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);
  37. }
  38. }
  39. // $file_upload_query="INSERT into user_uploads (`u_id`,`file_name`,`file_type`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
  40. //mysql_query($file_upload_query);
  41. }
  42. return "Success";
  43. }
  44. ?>

  45. 原文地址:http://www.freejs.net/article_biaodan_103.html

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template