-2

以下是我将文件移动到特定目录的代码。

foreach($files as $dir_file)    //Getting Path of XML file
        {
            echo "\nDir Files: ".$dir_file."\n";

            if($dir_file != "." && $dir_file != "..")
            {
                if(strpos($dir_file,'.xml'))     
                {
                    echo "\nXML file found\n";
                    $xmlPath=$path."/".$dir_file;
                    // return ReadXML($xmlPath);
                }
                if(strpos($dir_file,'.JPG'))
                {
                    echo "\n FOund \n";
                    $ret=move_uploaded_file($dir_file, 'upload/'.$dir_file));
                     echo "\n retunr values: ".$ret;
                }  
            }

我已经检查了所有权限,它是 777。但是移动上传文件功能不会将我的文件移动到特定目录。它也没有返回任何东西。我做错了什么?

4

2 回答 2

1

If you are to move files from one directory to another, you should use copy() function.

This is the example code:

$source = "YOUR_SOURCE_DIRECTORY/";
$destination = "YOUR_DESTINATION_DIRECTORY/";

foreach ($files as $file) {
    if (in_array($file, array(".","..")))
        continue;

    //If file is copied successfully then mark it for deletion
    if (copy($source.$file, $destination.$file)) {
        $delete[] = $source.$file;
    }
}
//If you want to delete the copied files then include these lines
//Delete all successfully copied files
foreach($delete as $file) {
    unlink($file);
}
于 2013-11-01T07:06:21.690 回答
0

move_uploaded_file

该函数检查以确保 filename 指定的文件是有效的上传文件(意味着它是通过 PHP 的 HTTP POST 上传机制上传的)。如果文件有效,它将被移动到目的地给定的文件名。

(强调我的。)您move_uploaded_file只能用于移动已上传的文件,其中“已上传”表示“PHP 已收到当前请求中的文件并且该文件当前位于$_FILES超级全局中”。任何其他文件都不能由move_uploaded_file. 这完全是故意的,move_uploaded_file应该保护您免受自己的伤害,并确保您只移动用户上传的文件。

如果您想移动任何其他不是刚刚通过 HTTP POST 上传的文件,请使用copyrename

于 2013-11-01T07:33:35.150 回答