Home  >  Article  >  Backend Development  >  Pure PHP multiple file upload_PHP tutorial

Pure PHP multiple file upload_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:38920browse

This example is just PHP, suitable for learning. This example can be added to the code of the database

When uploading multiple files, please select multiple files at once, support doc, jpg, pdf, etc.

This There are many practical multi-file uploads on the site. Here we recommend a "jQuery ajax multi-image upload without refreshing and recording to the database"

Pure PHP multiple file upload_PHP tutorialDemo

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. Original address: http://www.freejs. net/article_biaodan_103.html

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621645.htmlTechArticleThis example is just PHP, suitable for learning. This example can be added to the database code. Please upload multiple files once. Select multiple files, support doc jpg pdf and other more practical multiple files on this site...
Statement:
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