-4

如何删除维护php内容的目录?

例如:

我有这个包含文件的目录:

+ /home/user/files/arq1.mp3
+ /home/user/files/arq2.mp3
+ /home/user/files/arq3.mp3

PHP文件在:

+ /home/user/files.php

如何删除文件目录但保留 mp3 文件/home/user/ directory

4

2 回答 2

1

PHP方法:

foreach (glob("/home/user/files/*") as $file) {
    copy($file,"/home/user/".basename($file));
}
rmdir("/home/user/files/");

终端方法:

mv /home/user/files/* /home/user/*
rmdir /home/user/files/
于 2013-05-29T17:07:26.863 回答
1

*nix 方法:)

find . -type f | awk '{print "cp " $1 " ."}' | sh

如果您不确定它会先尝试底部的操作,那只会打印生成的操作。将它们连接到“sh”会执行它们。

find . -type f | awk '{print "cp " $1 " ."}'

好的:) PHP方法:

function flatten_structure( $path = './tree', $target_path = './flat' ) {
  $handle = opendir( $path );
  while( false !== ( $item = readdir( $handle ) ) ) {
    if ( is_file( $path . '/' . $item ) ) copy( $path . '/' . $item, $target_path );
    if ( is_dir( $path . '/' . $item ) ) flatten_structure( $path . '/' . $item, $target_path );
  }
}
于 2013-05-29T17:16:03.960 回答