PHP上传错误 - 为foreach()提供的参数无效

时间:2017-04-30 16:27:41

标签: php html5

我的照片上传表单出错了。 我有两个字段,一个用于多张照片,另一个用于存档。

<input type="file" name="file[]" multiple="multiple" class="custom-file-input" id="file_upload_gallery" accept="image/*"/>

<input type="file" name="file" class="custom-file-input" id="file_upload" accept="application/zip, application/rar" /> <span class="custom-file-control">

我的PHP脚本会抛出错误 - 为foreach()

提供的参数无效
<?php 
    if (!is_dir($_SERVER['DOCUMENT_ROOT']. '/user_uploads/' . $_POST['user_name'] . '/')){
        mkdir($_SERVER['DOCUMENT_ROOT']. '/user_uploads/' . $_POST['user_name'] . '/');
        foreach($_FILES['file']['name'] as $key=>$filename) {
            print_r($_FILES);
            if(move_uploaded_file($_FILES['file']['tmp_name'][$key], $_SERVER['DOCUMENT_ROOT']. '/user_uploads/' . $_POST['user_name'] . '/' . $filename)) {
                $uploaded[] = $filename; 
            }
        }
        echo json_encode($uploaded);    
    } else {
        foreach($_FILES['file']['name'] as $key=>$filename) {
            print_r($_FILES);
            if(move_uploaded_file($_FILES['file']['tmp_name'][$key], $_SERVER['DOCUMENT_ROOT']. '/user_uploads/' . $_POST['user_name'] . '/' . $filename)) {
                $uploaded[] = $filename; 
            }
        }
        echo json_encode($uploaded);            
    }
?>

哪里有问题?请帮忙! 谢谢!

1 个答案:

答案 0 :(得分:0)

对于两个输入使用同名文件的名称可能有问题。 而且对于这个

foreach($_FILES['file[]']['name'] as $key=>$filename) {

你需要写

foreach($_FILES['file']['name'] as $key=>$filename) { // Omit the [] from file