move_uploaded_file不将文件移动到文件夹

时间:2019-07-18 11:08:32

标签: php file upload

我有一个可以上传一些文件的表格。当我print时,文件数组显示正确,但是目标文件夹中没有文件。我想念什么?

这是我的代码:

// Fileupload
if (!is_dir('../incidentbijlagen/'.$inc['incident']['companyname'].'')) {
    mkdir('../incidentbijlagen/'.$inc['incident']['companyname'].'', 0777, true);
}

$uploads_dir = '../incidentbijlagen/'.$inc['incident']['companyname'].'/';

foreach($bijlage['incident'] as $key => $file){
  $tmp_name = $file['incident']['tmp_name']['bijlage'][$key];
  $name = basename($file['incident']['name']['bijlage'][$key]);
  move_uploaded_file($tmp_name, "$uploads_dir/$name");
}

我的error_log中没有错误,所以我不确定为什么不移动文件。这是我print时数组的样子:

Array
(
    [incident] => Array
        (
            [name] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => fblogosnm.jpg
                        )

                )

            [type] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => image/jpeg
                        )

                )

            [tmp_name] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => /tmp/phpnh5aHW
                        )

                )

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

                )

            [size] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => 20573
                        )

                )

        )

)

2 个答案:

答案 0 :(得分:0)

摆脱循环,尝试像这样使用0:

  $tmp_name = $file['incident']['tmp_name']['bijlage'][0];
  $name = basename($file['incident']['name']['bijlage'][0]);
  move_uploaded_file($tmp_name, "$uploads_dir/$name");

更新

可以多次输入,请尝试以下操作:

foreach($bijlage['incident']['name']['bijlage'] as $key => $file) {
  $tmp_name = $file['incident']['tmp_name']['bijlage'][$key];
  move_uploaded_file($tmp_name, "$uploads_dir/$file");
}

答案 1 :(得分:0)

尝试替换: $uploads_dir = '../incidentbijlagen/'.$inc['incident']['companyname'].'/';

具有: $uploads_dir = realpath('../incidentbijlagen/'.$inc['incident']['companyname'].'/');

这将返回完整路径(例如:/ var / www / yourwebsite / incidentbijlagen /等)