Php move_上传的文件将文件移动到目标,但返回false,为什么?

Php move_上传的文件将文件移动到目标,但返回false,为什么?,php,file-upload,multipartform-data,Php,File Upload,Multipartform Data,我在这里面临一个奇怪的情况:我有一个高级的多部分文件上载脚本,例如,它通过扫描目标文件夹来检查重复的文件名,然后用迭代编号重命名重复的文件名。现在,这里的问题是,由于某种原因,如果没有提交副本,脚本将以绿灯通过,但如果输入了副本,脚本将在move_upload_file部分返回false,但是,仍然能够在目标文件夹中创建正确的副本。我只是想知道,为什么move_upload_file函数返回false,但仍然继续移动文件 下面是脚本的简化片段,只是想指出问题所在: <?php //I'll

我在这里面临一个奇怪的情况:我有一个高级的多部分文件上载脚本,例如,它通过扫描目标文件夹来检查重复的文件名,然后用迭代编号重命名重复的文件名。现在,这里的问题是,由于某种原因,如果没有提交副本,脚本将以绿灯通过,但如果输入了副本,脚本将在move_upload_file部分返回false,但是,仍然能够在目标文件夹中创建正确的副本。我只是想知道,为什么move_upload_file函数返回false,但仍然继续移动文件

下面是脚本的简化片段,只是想指出问题所在:

<?php
//I'll loop all the files, which are submitted (in array)
foreach($_FILES['myFiles']['tmp_name'] as $key => $tmp_path) {

    //Alot of stuff (most likely unrelated) happens here

    //filepath contains both destination folder and filename
    $filepath = $destination_folder.$filename;

    if (file_exists($filepath)) {
        $duplicate_filename = true;

        //Some more stuff happens here. Then comes the actual moving part. Before this we have found duplicates 
        //for this upload file and counted proper duplicate value. 

        $file_increment = $num_of_filename_duplicates + 1;

        while ($duplicate_filename == true) {
            if(file_exists($filepath)) {
                //Separate filename parts and make new duplicate name with increment value
                $info = pathinfo($filename);
                $basename =  basename($filename,'.'.$info['extension']);
                $newfilename = $basename."(".$file_increment.").".$info['extension'];
                $filepath = $destination_folder.$newfilename;

                //Now, this returns me false, but still creates the file into destination
                if(move_uploaded_file($tmp_path, $filepath)) {
                    $file_success = true;
                    $file_increment++;
                }
                //So thats why this is true and I'll get the file_error
                else {
                    $file_error = "File error: Uploading of the file failed.";
                    break;
                }
            }
            else {
                $duplicate_filename = false;
            }
        }
    }
}
?>

唯一的原因可能是:

1) 如果文件名是有效的上载文件,但某些文件无法移动 原因是,不会发生任何操作,并且将返回move\u上传的\u file() 错。此外,将发出警告

2) 如果文件名不是有效的上载文件,则不会执行任何操作,并且 move\u上传的\u file()将返回FALSE

你的情况似乎是2。尝试在脚本顶部设置以下内容以查看错误:

error_reporting(E_ALL);
ini_set("display_errors", 1); 

不要试图寻找魔法。只需调试您的代码。

您如何知道它在目标目标中“创建正确的副本”,而不是简单地保留已经存在的副本?我不明白一件事:为什么您有
$file\u increment++在最后一个
if
语句的末尾?@Pekka,因为我可以从目标文件夹检查它。例如如果我已经有test1.txt,并且我上传了另一个test1.txt,脚本将创建一个新的test1(1).txt,并且不会覆盖重复的test1(1.txt)。但是
move\u uploaded\u file()
不会这样做,脚本会这样做。什么东西会返回false?@mastazi,是的,它在那里是无用的。这非常不同,$filepath将不再是相同的,我需要迭代文件的增量。我知道这一点,我没有得到任何错误。如果是第二种情况,为什么仍然上载文件?如果您
echo$filepath在调用
移动上传的文件之前,路径是否正确?是的,它刚好正确。现在我开始认为循环本身有问题。可能
move\u上传的\u file
使用相同的参数执行了两次,您只得到最后一个结果(应该失败)?这听起来很合乎逻辑。我会尽力调查的。