【问题标题】:Multiple files uploading in multidimensional array在多维数组中上传多个文件
【发布时间】:2015-12-30 15:17:34
【问题描述】:

我有一个用户可以上传文件的行数未知的 php 表单。为此,我使用了二维数组。

<form action="upload_file_module.php" method="post" enctype="multipart/form-data">
<? $i = 0; ?>
<table>
    <tr>
        <td>
            <input type="hidden" name="row[<? echo $i; ?>][row_name]" value = "one"/> One
        </td>
        <td>
            <input type="file" name="row[<? echo $i; ?>][fileToUpload]" >
        </td>
        <? 
            $i++;
        ?>
    </tr>

    <tr>
        <td>
            <input type="hidden" name="row[<? echo $i; ?>][row_name]" value = "two"/> Two
        </td>
        <td>
            <input type="file" name="row[<? echo $i; ?>][fileToUpload]" >
        </td>
        <? 
            $i++;
        ?>
    </tr>
</table>    

<input type="submit" value="Upload" name="submit">

所有文件都应保存在不同的文件夹中。

<?php
$unique_id = "folder";
$unique_id = $unique_id . '/';
foreach ( $_POST['row'] as $val )   {

    $target_dir = $unique_id;
    $target_dir = $target_dir . "/" . $val. "/";
    if (!file_exists($target_dir)) {
        mkdir($target_dir, 0777, true);
    }

    echo '<table>';
    echo '<tr>';
    echo '<td>', $val['row_name'], '</td>';
    echo '<td>', $val['fileToUpload'], '</td>';
    echo '</tr>';
    echo '</table>';

    $target_file = $target_dir . basename($_FILES[$val['fileToUpload']]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 900000000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.<br>";
        } else {
            echo "Sorry, there was an error uploading your file.<br>";
        }
    }      
}

但实际上它没有看到任何文件,并且输出看起来像:

一个

抱歉,文件已存在。抱歉,您的文件未上传。

两个

抱歉,文件已存在。抱歉,您的文件未上传。

【问题讨论】:

  • 那我们调试一下吧。您是否能够: 1) 制作目录? 2)给它它的权限? 3) 实际上传 tmp 文件夹的文件(还没有移动它)?
  • 1.是的。我能够创建文件夹。 2. 经许可一切正常。 3. 我没有将它们上传到 tmp 文件夹。问题是我无法从 $_FILES[$val['fileToUpload']]["name"] 获取它们,因为 $val['fileToUpload'] 是空的。
  • 上传后你var_dump你的$_FILES看它的结构了吗?
  • @AlanMachado 是的。这里的样子:imgur.com/1Dhj6Rd
  • 是的。我得到了问题的原因。非常感谢! @AlanMachado

标签: php arrays file-upload multidimensional-array foreach


【解决方案1】:

除了 cmets 中的更改之外,还有一些更改。主要问题是:

  1. $_POST$_FILES 在PHP 本质上是完全独立的实体时,您正试图将文件和数据发布到一个数组中。因此,当这两个超全局变量中实际上有两个 row 数组时,您最终会尝试访问单个数组 row
  2. 您的$target_file 从未声明过,您的$target_dir 有太多斜杠。
  3. 项目1让你以错误的方式访问$val

这些是我提出的最终更正,为您自己的环境保留逻辑。每个部分的解释都在代码里面注释掉了。

HTML 表单

<!DOCTYPE html>
<html>
<head></head>
<body>
	<form action="upload.php" method="post" enctype="multipart/form-data">
            <table>
	<?php $counter = 2; // number of fields you want to be printed
	for($i = 0; $i < $counter; $i++): // do proper counting ?>
                <tr>
			<td>
				<input type="hidden" name="row[]" value="<?php echo $i; ?>"/> File:
			</td>
			<td>
				<input type="file" name="row[]" >
			</td>
		</tr>
	<?php endfor; ?>
	    </table>
	    <input type="submit" value="Upload" name="submit">
	</form>
</body>
</html>

PHP 后期脚本

<?php
    $unique_id = "upload"; // just changed to reproduce
    $unique_id = $unique_id . '/';
    foreach ($_POST['row'] as $val)   {

        $target_dir = $unique_id;
        $target_file = $_FILES['row']['name'][$val]; //actually defining this var
        $target_dir = $target_dir . $val. "/"; // no need to index assoc nor '/'
        if (!file_exists($target_dir)) {
            mkdir($target_dir, 0777, true);
        }

        $imageFileType = pathinfo($target_dir,PATHINFO_EXTENSION);
        // Check if image file is a actual image or fake image

        // Check if file already exists
        if (file_exists($target_file)) {
            die("Sorry, file already exists."); // die if error
        }
        // Check file size
        if ($_FILES['row']['size'][$val] > 900000000) { // using the proper index reference for file
            die("Sorry, your file is too large."); //die if error
        }

        // Check if there are error msg in $_FILES
        if ($_FILES['row']['error'][$val] != 0) {
            die("Sorry, your file was not uploaded."); // die if error
            // if everything is ok, try to upload file
        } else {
            // point your move_files with the final name of the actual file to be moved
            if (move_uploaded_file($_FILES['row']['tmp_name'][$val], $target_dir.'/'.$_FILES['row']['name'][$val])) {
                echo "The file ". basename($_FILES['row']['name'][$val]). " has been uploaded.<br>";
            } else {
                die("Sorry, there was an error uploading your file.");
            }
        }
    }

最终输出(上传两个虚拟文件后)

Array
(
    [row] => Array
        (
            [0] => 0
            [1] => 1
        )

    [submit] => Upload
)
Array
(
    [row] => Array
        (
            [name] => Array
                (
                    [0] => dummyfile1.docx
                    [1] => dummyfile2.mpp
                )

            [type] => Array
                (
                    [0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
                    [1] => application/vnd.ms-project
                )

            [tmp_name] => Array
                (
                    [0] => C:\Windows\Temp\php73DA.tmp
                    [1] => C:\Windows\Temp\php73DB.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 0
                    [1] => 180224
                )

        )

)
The file dummyfile1.docx has been uploaded.
The file dummyfile2.mpp has been uploaded.

【讨论】:

  • 谢谢你,@Alan Machado。这会有所帮助。
猜你喜欢
  • 2012-04-14
  • 1970-01-01
  • 2022-11-17
  • 2012-05-31
  • 2013-09-30
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多