PHP上载多个文件仅上载1个文件

PHP上载多个文件仅上载1个文件,php,upload,Php,Upload,我多次编辑这段代码(imnoobwhitphp),我的问题是用这段代码上传多个文件。我只能上传一个文件 代码如下: <?php /** * uploadFile() * * @param string $file_field name of file upload field in html form * @param bool $check_image check if uploaded file is a valid image * @param bool $random_

我多次编辑这段代码(imnoobwhitphp),我的问题是用这段代码上传多个文件。我只能上传一个文件

代码如下:

<?php
/**
 * uploadFile()
 * 
 * @param string $file_field name of file upload field in html form
 * @param bool $check_image check if uploaded file is a valid image
 * @param bool $random_name generate random filename for uploaded file
 * @return array
 */
function uploadFile ($file_field = null, $check_image = false, $random_name = false) {

  //Config Section    
  //Set file upload path
  $path = 'c:/xampp/htdocs/'; //with trailing slash
  //Set max file size in bytes
  $max_size = 1000000;
  //Set default file extension whitelist
  $whitelist_ext = array('jpg','png','gif');
  //Set default file type whitelist
  $whitelist_type = array('image/jpeg', 'image/png','image/gif');

  //The Validation
  // Create an array to hold any output
  $out = array('error'=>null);

  if (!$file_field) {
    $out['error'][] = "Please specify a valid form field name";           
  }

  if (!$path) {
    $out['error'][] = "Please specify a valid upload path";               
  }

  if (count($out['error'])>0) {
    return $out;
  }

  //Make sure that there is a file
  if((!empty($_FILES[$file_field])) && ($_FILES[$file_field]['error'] == 0)) {

    // Get filename
    $file_info = pathinfo($_FILES[$file_field]['name']);
    $name = $file_info['filename'];
    $ext = $file_info['extension'];

    //Check file has the right extension           
    if (!in_array($ext, $whitelist_ext)) {
      $out['error'][] = "Invalid file Extension";
    }

    //Check that the file is of the right type
    if (!in_array($_FILES[$file_field]["type"], $whitelist_type)) {
      $out['error'][] = "Invalid file Type";
    }

    //Check that the file is not too big
    if ($_FILES[$file_field]["size"] > $max_size) {
      $out['error'][] = "File is too big";
    }

    //If $check image is set as true
    if ($check_image) {
      if (!getimagesize($_FILES[$file_field]['tmp_name'])) {
        $out['error'][] = "Uploaded file is not a valid image";
      }
    }

    //Create full filename including path
    if ($random_name) {
      // Generate random filename
      $tmp = str_replace(array('.',' '), array('',''), microtime());

      if (!$tmp || $tmp == '') {
        $out['error'][] = "File must have a name";
      }     
      $newname = $tmp.'.'.$ext;                                
    } else {
        $newname = $name.'.'.$ext;
    }

    //Check if file already exists on server
    if (file_exists($path.$newname)) {
      $out['error'][] = "A file with this name already exists";
    }

    if (count($out['error'])>0) {
      //The file has not correctly validated
      return $out;
    } 

    if (move_uploaded_file($_FILES[$file_field]['tmp_name'], $path.$newname)) {
      //Success
      $out['filepath'] = $path;
      $out['filename'] = $newname;
      return $out;
    } else {
      $out['error'][] = "Server Error!";
    }

  } else {
    $out['error'][] = "No file uploaded";
    return $out;
  }      
}
?>
<?php
if (isset($_POST['submit'])) {
  $file = uploadFile('file', true, true);
  if (is_array($file['error'])) {
    $message = '';
    foreach ($file['error'] as $msg) {
      $message .= '<p>'.$msg.'</p>';    
    }
  } else {
    $message = "File uploaded successfully";
  }
  echo $message;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input name="file" type="file" size="20" multiple="multiple" />
<input name="submit" type="submit" value="Upload files" />
</form>


您必须使用foreach循环上载多个文件。在框架中,您还提供了具有相同功能的组件(即通过使用foreach循环)

您应该使用file字段作为数组,如
file[]

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <input name="file[]" type="file" size="20" multiple="multiple" />
    <input name="submit" type="submit" value="Upload files" />
</form>

正如其他人所指出的,您需要将上载字段名更改为ie.files[](名称后面包括sqaure括号)。这是相关的,因为它告诉PHP应该将字段视为数组

此外,在代码中,您可以使用foreach()访问上传的文件,如下所示:

foreach ($_FILES['field_name'] as $file)
    ["name"]=>
    array(2) {
      [0]=>
      string(5) "dir.c"
      [1]=>
      string(10) "errcodes.h"
    }
    ["type"]=>
    array(2) {
      [0]=>
      string(11) "text/x-csrc"
      [1]=>
      string(11) "text/x-chdr"
    }
    ["tmp_name"]=>
    array(2) {
      [0]=>
      string(14) "/tmp/phpP1iz5A"
      [1]=>
      string(14) "/tmp/phpf31fzn"
    }
    ["error"]=>
    array(2) {
      [0]=>
      int(0)
      [1]=>
      int(0)
    }
    ["size"]=>
    array(2) {
      [0]=>
      int(511)
      [1]=>
      int(38)
    }
  }
(显然,在本例中,yout html字段将是names字段_name[] 这将在五次迭代中每次返回一个数组,为您提供有关已发送的所有文件的信息。例如,如果您发送了两个文件,则可能如下所示:

foreach ($_FILES['field_name'] as $file)
    ["name"]=>
    array(2) {
      [0]=>
      string(5) "dir.c"
      [1]=>
      string(10) "errcodes.h"
    }
    ["type"]=>
    array(2) {
      [0]=>
      string(11) "text/x-csrc"
      [1]=>
      string(11) "text/x-chdr"
    }
    ["tmp_name"]=>
    array(2) {
      [0]=>
      string(14) "/tmp/phpP1iz5A"
      [1]=>
      string(14) "/tmp/phpf31fzn"
    }
    ["error"]=>
    array(2) {
      [0]=>
      int(0)
      [1]=>
      int(0)
    }
    ["size"]=>
    array(2) {
      [0]=>
      int(511)
      [1]=>
      int(38)
    }
  }
重要的是要理解,PHP不会将这些文件分类,然后为每个文件提供属性,而是列出所有文件的属性

我希望现在清楚了。

我的建议是

  • 启动一个循环:
    foreach($\u FILES[$file\u field]作为$file){
  • 在函数末尾用
    }
    将其关闭
  • 将其中的所有
    $\u文件[$file\u字段]
    更改为
    $file
  • 当然,输入必须具有
    multiple
    属性,如Sherin Josesaid(),但现在它只受多个浏览器的完全支持,所以最好用JS添加更多输入,如

    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    ...
    
    
    ...
    

    并以相同的方式循环它们

    要从浏览器成功发送多个文件,需要输入将数组传递给PHP。这是通过将
    []
    附加到您的
    姓名的末尾来完成的:

    <input type="file" name="filesToUpload[]" multiple>
    

    要运行此示例,您的文件应该如下所示

    您可以使用以下命令启动PHP内置Web服务器:

    $ php -S localhost:8000
    
    然后将运行该示例



    重要注意事项:上述示例不进行任何验证。您需要验证所有上传的文件是否安全。

    multiple仍然不需要完全是HTML5的
    multiple=“multiple”
    只有
    multiple
    就足够了,您还可以对同名的多个字段使用
    []
    ,但您不需要它,当您仅对1个字段使用
    multiple
    属性时,但我认为您不会获得所有文件,只会生成一个文件..尝试一下..这次您是对的,但很困惑,为什么要使用两个修改?看起来像是HTML5 BugMultiple属性仅用于使用单个文件字段保存多个文件,但如果没有数组类型,php总是将其作为单个文件处理。。。