以下是我将文件移动到特定目录的代码段。
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。 但是移动上传文件功能不会将我的文件移动到特定目录。 它也没有返回任何内容。我做错了什么?
如果要
将文件从一个目录移动到另一个目录,则应使用 copy() 函数。
这是示例代码:
$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);
}
move_uploaded_file
此函数检查以确保文件名指定的文件是有效的上传文件(这意味着它是通过 PHP 的 HTTP POST 上传机制上传的)。如果文件有效,它将被移动到目的地给出的文件名。
(强调我的。您只能使用 move_uploaded_file
来移动已上传的文件,其中"已上传"表示"PHP 已收到当前请求中的文件,并且该文件当前处于$_FILES
超级全局中"。任何其他文件都不能通过 move_uploaded_file
移动。这完全是故意的,move_uploaded_file
应该保护您免受自己的伤害,并确保您只移动用户上传的文件。
如果要移动任何其他文件,而不是通过HTTP POST上传的文件,请使用copy
或rename
。