在for循环中的文件中上传

时间:2017-08-08 09:35:47

标签: php html upload

我正在使用此上传脚本http://www.dropzonejs.com

上传php部分是:

foreach($_POST["id"] as $key=>$value)
{
    $id = trim(mysqli_real_escape_string($mysqli, $value));
    $pre_set = trim(mysqli_real_escape_string($mysqli, $_POST['pre_set'][$key]));
    $keep_filename = trim(mysqli_real_escape_string($mysqli, $_POST['keep_filename'][$key]));

    $ds          = DIRECTORY_SEPARATOR;

    $storeFolder = '../uploads';

    if (!empty($_FILES))
    {

        $tempFile = $_FILES['file']['tmp_name'];

        $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;

        if($keep_filename == 'yes')
        {
            $targetFile =  $targetPath. $pre_set.'_'.$id.'_'.$_FILES['file']['name'];
        }
        else
        {
            $targetFile =  $targetPath. $pre_set.'_'.$id.'.'.pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        }

        move_uploaded_file($tempFile,$targetFile);

    }
}

使用标准表单时,如下所示,文件已正确上传并重命名:

<form action="includes/upload.php" class="dropzone">

<input type="hidden" class="form-control" id="id[]" name="id[]" value="'.$row['id'].'">
<input type="hidden" class="form-control" id="pre_set[]" name="pre_set[]" value="logo">
<input type="hidden" class="form-control" id="keep_filename[]" name="keep_filename[]" value="no">

</form>

但是当使用for循环情况时,图像会显示在上传窗口中,但文件不会上传。 我没有收到任何错误(或者没有使用正确的参数来查看)

while($row = mysqli_fetch_array($res))
{ $i++;
    echo'

 <div class="container">

<form action="includes/upload.php">

    <div class="form-group col-md-3">';
      if($i == 1) { echo '<label>Voeg foto toe</label>'; } echo'
        <div class="dropzone dropzone_small" id="myId'.$i.'">
            <div class="fallback">
                <input type="hidden" class="form-control" id="id[]" name="id[]" value="xxxx">
                <input type="hidden" class="form-control" id="pre_set[]" name="pre_set[]" value="toolbox_">
                <input type="hidden" class="form-control" id="keep_filename[]" name="keep_filename[]" value="yes">
            </div>
        </div>
    </div>
</form>';
}

有任何改变代码以使其正常工作的建议吗?

非常感谢帮助。

1 个答案:

答案 0 :(得分:-1)

您必须使用

开始表单
<form action="includes/upload.php"> method="post" enctype="multipart/form-data">

//此处的表单属性

</form>