在php中多文件上传 [英] Multiple file upload in php

查看:93
本文介绍了在php中多文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传多个文件并将它们存储在一个文件夹中,并获取路径并将其存储在数据库中...任何您想要做多个文件上传的好例子...



注意:文件可以是任何类型的文件...

解决方案

知道这是一个旧的帖子,但一些进一步的解释可能是有用的人试图上传多个文件...这是你需要做的:


  • 输入名称必须定义为数组,例如
    name =inputName []

  • 输入元素必须有多个=multiple多重

  • 在您的PHP文件中使用$ _ FILES ['inputName'] ['param '] [index]

  • 确保查找 空文件名和路径 ,数组可能包含空字符串。在计数之前使用array_filter()。


    这里是一个简单的例子(显示相关代码)

    HTML:

     < input name =upload []type =filemultiple =multiple/> 

    PHP:

      // $ files = array_filter($ _ FILES ['upload'] ['name']);类似于在处理文件之前使用的东西。 
    //计算上传文件的数量
    $ total = count($ _ FILES ['upload'] ['name']);

    //遍历每个文件
    ($ i = 0; $ i <$ total; $ i ++){
    //获取临时文件路径
    $ tmpFilePath = $ _FILES ['upload'] ['tmp_name'] [$ i]; ($ tmpFilePath!=){
    //设置我们的新文件路径
    $ newFilePath =。 /上传文件/ 。 $ _FILES [上传] [名] [$ i];

    //将文件上传到临时目录
    if(move_uploaded_file($ tmpFilePath,$ newFilePath)){

    //在此处处理其他代码

    }
    }
    }


    I want to upload multiple files and store them in a folder and get the path and store it in the database... Any good example you looked for doing multiple file upload...

    Note: Files can be of any type...

    解决方案

    I know this is a old post but some further explanation might be useful for someone trying to upload multiple files... Here is what you need to do:

    • Input name must be be defined as an array i.e. name="inputName[]"
    • Input element must have multiple="multiple" or just multiple
    • In your PHP file use the syntax "$_FILES['inputName']['param'][index]"
    • Make sure to look for empty file names and paths, the array might contain empty strings. Use array_filter() before count.

    here is a down and dirty example (showing just relevant code)

    HTML:

    <input name="upload[]" type="file" multiple="multiple" />
    

    PHP:

    //$files = array_filter($_FILES['upload']['name']); something like that to be used before processing files.
    // Count # of uploaded files in array
    $total = count($_FILES['upload']['name']);
    
    // Loop through each file
    for($i=0; $i<$total; $i++) {
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
      //Make sure we have a filepath
      if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
    
        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
    
          //Handle other code here
    
        }
      }
    }
    

    Hope this helps out!

    这篇关于在php中多文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆