Php 多维数组文件上载

Php 多维数组文件上载,php,arrays,file-upload,multidimensional-array,upload,Php,Arrays,File Upload,Multidimensional Array,Upload,我目前正在编写一个脚本,用户可以在其中向表单添加字段组并包含图像 问题是我不知道从哪里开始上传多维数组中的文件 到目前为止,我已经构建了表单:并让它与添加字段组和创建数组一起工作,但到目前为止,还没有任何关于上传文件的内容。我所能看到的就是它将文件名作为字符串存储在数组中,就是这样 有没有一种方法可以使用foreach语句,而只使用普通的使用php上传文件的方法?类似于以下内容的修改版本: if ($_FILES["file"]["error"] > 0) { echo "Retu

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

到目前为止,我已经构建了表单:并让它与添加字段组和创建数组一起工作,但到目前为止,还没有任何关于上传文件的内容。我所能看到的就是它将文件名作为字符串存储在数组中,就是这样

有没有一种方法可以使用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"];
    }
  }
if($\u文件[“文件”][“错误”]>0){
回显“返回代码:”.$\u文件[“文件”][“错误”]。
”; }否则{ echo“上传:.”文件[“文件”][“名称”]。“
”; echo“Type:”.$\u文件[“file”][“Type”]。“
”; 回显“大小:”($_文件[“文件”][“大小”]/1024)。“kB
”; 回显“临时文件:”.$\u文件[“文件”][“tmp\u名称”]。
”; 如果(文件存在(“上载/”$\u文件[“文件”][“名称”])){ echo$\u文件[“文件”][“名称”]。“已存在。”; }否则{ 移动上传的文件($文件[“文件”][“tmp文件名”], “上载/”$_文件[“文件”][“名称”]); echo“存储在:“.”upload/“$\u文件[”文件“][”名称“]; } }

我遇到的唯一问题是,当表单发布时,$\u文件中没有任何内容,它只是一个空数组

这是最后一次编辑-

您的表单中缺少“enctype”选项:

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

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

服务器端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>

很高兴你解决了它:-)