php 将文件夹中的所有文件移动到另一个文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2082138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
move all files in a folder to another?
提问by ajsie
when moving one file from one location to another i use
将一个文件从一个位置移动到另一个位置时,我使用
rename('path/filename', 'newpath/filename');
how do you move all files in a folder to another folder? tried this one without result:
如何将一个文件夹中的所有文件移动到另一个文件夹?试过这个没有结果:
rename('path/*', 'newpath/*');
回答by Sampson
A slightly verbose solution:
一个稍微冗长的解决方案:
// Get array of all source files
$files = scandir("source");
// Identify directories
$source = "source/";
$destination = "destination/";
// Cycle through all source files
foreach ($files as $file) {
if (in_array($file, array(".",".."))) continue;
// If we copied this successfully, mark it for deletion
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
// Delete all successfully-copied files
foreach ($delete as $file) {
unlink($file);
}
回答by GreenMatt
An alternate using rename() and with some error checking:
使用 rename() 并进行一些错误检查的替代方法:
$srcDir = 'dir1';
$destDir = 'dir2';
if (file_exists($destDir)) {
if (is_dir($destDir)) {
if (is_writable($destDir)) {
if ($handle = opendir($srcDir)) {
while (false !== ($file = readdir($handle))) {
if (is_file($srcDir . '/' . $file)) {
rename($srcDir . '/' . $file, $destDir . '/' . $file);
}
}
closedir($handle);
} else {
echo "$srcDir could not be opened.\n";
}
} else {
echo "$destDir is not writable!\n";
}
} else {
echo "$destDir is not a directory!\n";
}
} else {
echo "$destDir does not exist\n";
}
回答by Lakhan
Please try this solution, it's tested successfully ::
请尝试此解决方案,它已成功测试::
<?php $files = scandir("f1"); $oldfolder = "f1/"; $newfolder = "f2/"; foreach($files as $fname) { if($fname != '.' && $fname != '..') { rename($oldfolder.$fname, $newfolder.$fname); } } ?>
<?php $files = scandir("f1"); $oldfolder = "f1/"; $newfolder = "f2/"; foreach($files as $fname) { if($fname != '.' && $fname != '..') { rename($oldfolder.$fname, $newfolder.$fname); } } ?>
回答by Juergen
Move or copy the way I use it
移动或复制我使用它的方式
function copyfiles($source_folder, $target_folder, $move=false) {
$source_folder=trim($source_folder, '/').'/';
$target_folder=trim($target_folder, '/').'/';
$files = scandir($source_folder);
foreach($files as $file) {
if($file != '.' && $file != '..') {
if ($move) {
rename($source_folder.$file, $target_folder.$file);
} else {
copy($source_folder.$file, $target_folder.$file);
}
}
}
}
function movefiles($source_folder, $target_folder) {
copyfiles($source_folder, $target_folder, $move=true);
}
回答by T.Todua
tried this one?:
试过这个?:
<?php
$oldfolderpath = "old/folder";
$newfolderpath = "new/folder";
rename($oldfolderpath,$newfolderpath);
?>
回答by paviktherin
So I tried to use the rename()function as described and I kept getting the error back that there was no such file or directory. I placed the code within an if else statement in order to ensure that I really did have the directories created. It looked like this:
因此,我尝试使用rename()所描述的函数,但我不断收到错误消息,即存在no such file or directory. 我将代码放在 if else 语句中,以确保我确实创建了目录。它看起来像这样:
$tempDir = '/home/site/images/tmp/';
$permanentDir = '/home/site/images/' . $claimid; // this was stored above
mkdir($permanentDir,0775);
if(is_dir($permanentDir)){
echo $permanentDir . ' is a directory';
if(is_dir($tempDir)){
echo $tempDir . ' is a directory';
}else{
echo $tempDir . ' is not a directory';
}
}else{
echo $permanentDir . ' is not a directory';
}
rename($tempDir . "*", $permanentDir);
So when I ran the code again, it spit out that both paths were directories. I was stumped. I talked with a coworker and he suggested, "Why not just rename the temp directory to the new directory, since you want to move all the files anyway?"
所以当我再次运行代码时,它吐出两个路径都是目录。我被难住了。我与一位同事交谈,他建议:“为什么不将临时目录重命名为新目录,因为您无论如何都想移动所有文件?”
Turns out, this is what I ended up doing. I gave up trying to use the wildcard with the rename()function and instead just use the rename()to rename the temp directory to the permanent one.
事实证明,这就是我最终要做的。我放弃了在rename()函数中使用通配符的尝试,而是使用rename()将临时目录重命名为永久目录。
so it looks like this.
所以它看起来像这样。
$tempDir = '/home/site/images/tmp/';
$permanentDir = '/home/site/images/' . $claimid; // this was stored above
mkdir($permanentDir,0775);
rename($tempDir, $permanentDir);
This worked beautifully for my purposes since I don't need the old tmp directory to remain there after the files have been uploaded and "moved".
这对我的目的非常有效,因为在上传和“移动”文件后,我不需要旧的 tmp 目录保留在那里。
Hope this helps. If anyone knows why the wildcard doesn't work in the rename()function and why I was getting the error stating above, please, let me know.
希望这可以帮助。如果有人知道为什么通配符在rename()函数中不起作用以及为什么我收到上述错误,请告诉我。
回答by trante
As a side note; when you copy files to another folder, their last changed time becomes current timestamp. So you should touch()the new files.
作为旁注;当您将文件复制到另一个文件夹时,它们上次更改的时间将成为当前时间戳。所以你应该touch()新文件。
... (some codes for directory looping) ...
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
$filetimestamp = filemtime($source.$file);
touch($destination.$file,$filetimestamp);
}
... (some codes) ...
回答by Aadith Ramia
try this: rename('path/*', 'newpath/');
试试这个: rename('path/*', 'newpath/');
I do not see a point in having an asterisk in the destination
我不认为在目的地有星号有什么意义
回答by Adam Hopkinson
If the target directory doesn't exist, you'll need to create it first:
如果目标目录不存在,您需要先创建它:
mkdir('newpath');
rename('path/*', 'newpath/');
回答by senya
Not sure if this helps anyone or not, but thought I'd post anyway. Had a challenge where I has heaps of movies I'd purchased and downloaded through various online stores all stored in one folder, but all in their own subfolders and all with different naming conventions. I wanted to move all of them into the parent folder and rename them all to look pretty. all of the subfolders I'd managed to rename with a bulk renaming tool and conditional name formatting. the subfolders had other files in them i didn't want. so i wrote the following php script to, 1. rename/move all files with extension mp4 to their parent directory while giving them the same name as their containing folder, 2. delete contents of subfolders and look for directories inside them to empty and then rmdir, 3. rmdir the subfolders.
不确定这是否对任何人有帮助,但我想我还是会发帖。我遇到了一个挑战,我通过各种在线商店购买和下载的大量电影都存储在一个文件夹中,但都存储在它们自己的子文件夹中,并且都具有不同的命名约定。我想将它们全部移到父文件夹中并将它们全部重命名以使其看起来很漂亮。我设法使用批量重命名工具和条件名称格式重命名的所有子文件夹。子文件夹中有我不想要的其他文件。所以我写了以下 php 脚本,1. 将所有扩展名为 mp4 的文件重命名/移动到它们的父目录,同时为它们提供与其包含文件夹相同的名称,2. 删除子文件夹的内容并查找其中的目录为空,然后rmdir, 3. rmdir 子文件夹。
$handle = opendir("D:/Movies/");
while ($file = readdir($handle)) {
if ($file != "." && $file != ".." && is_dir($file)) {
$newhandle = opendir("D:/Movies/".$file);
while($newfile = readdir($newhandle)) {
if ($newfile != "." && $newfile != ".." && is_file("D:/Movies/".$file."/".$newfile)) {
$parts = explode(".",$newfile);
if (end($parts) == "mp4") {
if (!file_exists("D:/Movies/".$file.".mp4")) {
rename("D:/Movies/".$file."/".$newfile,"D:/Movies/".$file.".mp4");
}
else {
unlink("D:/Movies/".$file."/".$newfile);
}
}
else { unlink("D:/Movies/".$file."/".$newfile); }
}
else if ($newfile != "." && $newfile != ".." && is_dir("D:/Movies/".$file."/".$newfile)) {
$dirhandle = opendir("D:/Movies/".$file."/".$newfile);
while ($dirfile = readdir($dirhandle)){
if ($dirfile != "." && $dirfile != ".."){
unlink("D:/Movies/".$file."/".$newfile."/".$dirfile);
}
}
rmdir("D:/Movies/".$file."/".$newfile);
}
}
unlink("D:/Movies/".$file);
}
}

