0

我有以下上传单个文件并且工作正常:

<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    <input name="userfile" type="file" />
    <input type="submit" value="Upload" />
</form>
<?php

$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File has been successfully uploaded.\n";
} else {
  echo "Upload failed";
}


?> 

当我调整它以接受多个文件进行上传时,它似乎不起作用。我没有收到任何错误/警告,所以我完全被难住了。这是我的多文件上传代码:

<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    <input name="userfile[]" type="file" multiple />
    <input type="submit" value="Upload" />
</form>

<?php 

$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);

foreach ($_FILES["userfile"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
$name = $_FILES["userfile"]["name"][$key]; 
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile); 
{
 echo "File has been successfully uploaded.\n";
 } else {
 echo "Upload failed";
 }
?>

关于什么可能是错的任何建议?

4

2 回答 2

1

您使用了错误的键$_FILES,您必须使用$_FILES['userfile'],而不是$_FILES['files']

<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    <input name="userfile[]" type="file" multiple />
    <input type="submit" value="Upload" />
</form>

<?php 

$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);

foreach ($_FILES["userfile"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
$name = $_FILES["userfile"]["name"][$key]; 
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile); 
{
 echo "File has been successfully uploaded.\n";
 } else {
 echo "Upload failed";
 }
?>
于 2013-11-04T10:17:28.210 回答
0
          <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <title>Multiple File Ppload with PHP</title>
    </head>
    <body>
      <form action="" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
      <input type="submit" value="Upload!" />
    </form>
    </body>
    </html>


      <?php

         $valid_formats = array("jpg", "png", "gif", "zip", "bmp");
        $max_file_size = 1024*100; //100 kb
        $path = "uploads/"; // Upload directory
        $count = 0;

        if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
            // Loop $_FILES to exeicute all files
            foreach ($_FILES['files']['name'] as $f => $name) {     
                if ($_FILES['files']['error'][$f] == 4) {
                    continue; // Skip file if any error found
                }          
                if ($_FILES['files']['error'][$f] == 0) {              
                    if ($_FILES['files']['size'][$f] > $max_file_size) {
                        $message[] = "$name is too large!.";
                        continue; // Skip large files
                    }
                    elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                        $message[] = "$name is not a valid format";
                        continue; // Skip invalid file formats
                    }
                    else{ // No error found! Move uploaded files 
                        if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                        $count++; // Number of successfully uploaded file
                    }
                }
            }
        }
?>
于 2013-11-04T10:54:05.290 回答