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

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

问题描述

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

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 an old post but some further explanation might be useful for someone trying to upload multiple files... Here is what you need to do:

  • 输入名称必须定义为一个数组,即name="inputName[]"
  • 输入元素必须有 multiple="multiple" 或只有 multiple
  • 在你的 PHP 文件中使用语法 "$_FILES['inputName']['param'][index]"
  • 确保查找空文件名和路径,该数组可能包含空字符串.在计数之前使用 array_filter().
  • 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 file path
  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

    }
  }
}

希望这会有所帮助!

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

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