多维数组文件上传


Multidimensional array file upload

我目前正在编写一个脚本,用户可以在其中添加字段组到表单并包含图像问题是我不知道从哪里开始在多维数组中上传文件。

到目前为止,我已经构建了表单:http://letsfixit.co.uk/fortest.php并使其与添加字段组和创建数组一起工作,但到目前为止还没有得到文件上传。我所能看到的是它将文件名作为字符串存储在数组中,就是这样!

是否有一种方法来使用foreach语句,只是使用文件上传使用php的正常方法?类似于:

的修改版本
if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
  }

我唯一的问题是,当表单张贴没有任何在$_FILES,它只是一个空数组

这是最后的编辑-

你的表单缺少'enctype'选项:

我无法让你的php脚本工作。这里有一个方法……

在我看来,你为客户工作太辛苦了。PHP将为您整理输入字段数组。目前,在客户机上,您试图通过自己生成索引来组织每个步骤的所有数据。我建议不要麻烦了。使用数组小部件名称,但是固定名称,并收集服务器端每个步骤的所有数据。它简化了你的javascript,因为你只是复制字段组,但不改变名称。

Serverside PHP将所有输入安排在单独的数组中。这很好,因为每个数组中的第一行用于步骤1,第二行用于"步骤2"等。

无论如何,这里是一个"两步"的形式和上传脚本的工作,并已经过测试。

<?php
if (!empty($_FILES)) {
    // var_dump($_POST);
    // foreach($_FILES as $key => $value){
    //    var_dump($key, 'the key', $value, 'the value');
    // }
    // var_dump($_FILES["steps"]["error"]["image"], '$_FILES["steps"]["error"]["image"]');
    // all valid steps in here...
    $theOutputSteps = array();
    /*
     * Please note there are separate arrays.
     * But any ONE index value across all arrays is the complete record!
     *
     * This works 'cos empty values still come in and php makes them...
     */
    // we need to pick something to drive off... I use '$_POST[steps][title].
    // It does not matter as all the input arrays are the exact same size.
    /*
     * let us start to reassemble the information into a useable form...
     */
    foreach($_POST['steps']['title'] as $key => $value) {
        $curStep = array();
        $curStep['title'] = $value;
        $curStep['description'] = $_POST['steps']['description'][$key];
        // file details...
        $curStep['image']['error'] = $_FILES["steps"]["error"]["image"][$key];
        if (!$curStep['image']['error']) { // save the file
            $curStep['image']['name']      = $_FILES["steps"]["name"]["image"][$key];
            $curStep['image']['type']      = $_FILES["steps"]["type"]["image"][$key];
            $curStep['image']['size']      = $_FILES["steps"]["size"]["image"][$key];
            $curStep['image']['tmp_name']  = $_FILES["steps"]["tmp_name"]["image"][$key];
            if (!file_exists('./upload/'.$curStep['image']['name'])) {
                 move_uploaded_file($curStep['image']['tmp_name'],
                 './upload/'.$curStep['image']['name']);
            }
            else {
                 $curStep['image']['error'] = "file already exists: ". $curStep['image']['name'];
            }
        }
        $theOutputSteps[] = $curStep;
    }
    var_dump($theOutputSteps, '$theOutputSteps');
}
?>
<form id="steps" method="POST" action="" enctype="multipart/form-data">
    <div id="p_scents">
        <p>
                <label for="p_title_scnt"><input type="text" id="p_title_scnt" size="20" name="steps[title][]" value="Step 1" placeholder="Input Value" /></label> <br />
                <label for="p_step_scnt"><textarea id="p_step_scnt" name="steps[description][]" ></textarea></label> <br />
                <label for="p_image_scnt"><input type="file" id="p_image_scnt" name="steps[image][]" value="" /></label>
        </p>
        <p>
                <label for="p_title_scnt"><input type="text" id="p_title_scnt" size="20" name="steps[title][]" value="Step 2" placeholder="Input Value" /></label> <br />
                <label for="p_step_scnt"><textarea id="p_step_scnt" name="steps[description][]" ></textarea></label> <br />
                <label for="p_image_scnt"><input type="file" id="p_image_scnt" name="steps[image][]" value="" /></label>
        </p>
    </div>
    <a href="#" id="addScnt">Add Another Input Box</a><br /><br />
    <input type="submit" value="Post Guide">
</form>