将文件夹中的所有文件移动到另一个文件夹?

时间:2022-04-23 11:13:25

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/*');

9 个解决方案

#1


51  

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);
}

#2


15  

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";
}

#3


11  

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);
      }
  }
?>

#4


1  

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()函数来描述,并且不断地得到错误,因为没有这样的文件或目录。我将代码放在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()将临时目录重命名为permanent目录。

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()函数中不起作用,以及为什么我得到上面声明的错误,请告诉我。

#5


1  

tried this one?:

试过这个吗?

     <?php

     $oldfolderpath = "old/folder";
     $newfolderpath = "new/folder";

     rename($oldfolderpath,$newfolderpath);
     ?>

#6


0  

try this: rename('path/*', 'newpath/');

试试这个:重命名('路径/ * ',' newpath / ');

I do not see a point in having an asterisk in the destination

我不认为在目的地有星号是有意义的

#7


0  

If the target directory doesn't exist, you'll need to create it first:

如果目标目录不存在,您需要首先创建它:

mkdir('newpath');
rename('path/*', 'newpath/');

#8


0  

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.

作为边注;当您将文件复制到另一个文件夹时,它们最后更改的时间将成为当前时间戳。所以应该触摸()新文件。

... (some codes for directory looping) ...
if (copy($source.$file, $destination.$file)) {
   $delete[] = $source.$file;

   $filetimestamp = filemtime($source.$file); 
   touch($destination.$file,$filetimestamp);
}
... (some codes) ...

#9


0  

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的所有文件重命名为它们的父目录,同时给它们提供与包含文件夹相同的名称。删除子文件夹的内容,并查找其中的目录为空,然后是rmdir, 3。删除文件夹的子文件夹。

$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);
}
}

#1


51  

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);
}

#2


15  

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";
}

#3


11  

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);
      }
  }
?>

#4


1  

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()函数来描述,并且不断地得到错误,因为没有这样的文件或目录。我将代码放在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()将临时目录重命名为permanent目录。

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()函数中不起作用,以及为什么我得到上面声明的错误,请告诉我。

#5


1  

tried this one?:

试过这个吗?

     <?php

     $oldfolderpath = "old/folder";
     $newfolderpath = "new/folder";

     rename($oldfolderpath,$newfolderpath);
     ?>

#6


0  

try this: rename('path/*', 'newpath/');

试试这个:重命名('路径/ * ',' newpath / ');

I do not see a point in having an asterisk in the destination

我不认为在目的地有星号是有意义的

#7


0  

If the target directory doesn't exist, you'll need to create it first:

如果目标目录不存在,您需要首先创建它:

mkdir('newpath');
rename('path/*', 'newpath/');

#8


0  

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.

作为边注;当您将文件复制到另一个文件夹时,它们最后更改的时间将成为当前时间戳。所以应该触摸()新文件。

... (some codes for directory looping) ...
if (copy($source.$file, $destination.$file)) {
   $delete[] = $source.$file;

   $filetimestamp = filemtime($source.$file); 
   touch($destination.$file,$filetimestamp);
}
... (some codes) ...

#9


0  

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的所有文件重命名为它们的父目录,同时给它们提供与包含文件夹相同的名称。删除子文件夹的内容,并查找其中的目录为空,然后是rmdir, 3。删除文件夹的子文件夹。

$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);
}
}