0

ここで奇妙な状況に直面しています。たとえば、宛先フォルダーをスキャンして重複するファイル名をチェックし、重複する名前の名前を反復番号に変更する、高度なマルチパートファイルアップロードスクリプトがあります。ここでの問題は、何らかの理由で、重複が送信されない場合、スクリプトは緑色のライトで通過しますが、重複が入力された場合、スクリプトは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;
            }
        }
    }
}
?>
4

1 に答える 1

0

唯一の理由は次のとおりです。

1) filename が有効なアップロード ファイルであるが、何らかの理由で移動できない場合、アクションは発生せず、move_uploaded_file() は FALSE を返します。さらに、警告が発行されます。

2) filename が有効なアップロード ファイルでない場合、アクションは発生せず、move_uploaded_file() は FALSE を返します。

あなたのケースは 2 のようです。スクリプトの先頭で次のように設定して、エラーを確認してください。

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

魔法を見つけようとしないでください。コードをデバッグするだけです。

于 2012-12-04T11:05:19.170 に答える