我想移动所有文件EXCEPT .php文件
下面的代码移动所有文件,我只是不知道如何添加它以跳过所有.php文件。
// 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 );
}
答案 0 :(得分:1)
if(substr($file, -4) != '.php') {
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
答案 1 :(得分:1)
PHP具有很好的功能,可以帮助您仅捕获所需的文件。它被称为glob()
,
glob - 查找与模式匹配的路径名 以下是一个示例用法 -
$phpfiles = array();
foreach (glob("/path/to/folder/*.php") as $file) {
$files[] = $file;
}
$allfiles = scandir("/path/to/folder");
$files_that_are_not_php = array_diff($allfiles, $phpfiles);
//move the files in $files_that_are_not_php array
参考 -
答案 2 :(得分:1)
尝试以这种方式排除$ files数组循环中的 .php 文件。
第一路
$exclude=array('.php'); // add more extension if .
foreach ( $files as $file ) {
if(is_file($filepath)) {
$ext = getFileExtension($filename);
// execute code to list the file or whatever
if (!in_array($ext,$exclude)) {
// Code for files not .php file extension
}
}
}
function getFileExtension($filename) {
$path_info = pathinfo($filename);
return $path_info['extension'];
}
第二路
$files_without_php= preg_grep('~\.[^(php)]$~',
scandir($dir_f));