PHP使用特定格式移动文件与移动所有文件
我在/public\u html/目录中有以下文件:PHP使用特定格式移动文件与移动所有文件,php,Php,我在/public\u html/目录中有以下文件: 0832.php 1481.php 2853.php 3471.php index.php 我想把所有那些XXXX.php(总是4位格式)移到目录/tmp/,除了index.php。如何使用reg ex和loop执行此操作 或者,先将所有文件(包括index.php)移动到/tmp/中,然后再将index.php放回/public_html/,您认为哪个文件占用的CPU更少 最后一件事,我发现本教程使用PHP移动文件: 但是如何移动目录中的
0832.php
1481.php
2853.php
3471.php
index.php
我想把所有那些XXXX.php(总是4位格式)移到目录/tmp/,除了index.php。如何使用reg ex和loop执行此操作
或者,先将所有文件(包括index.php)移动到/tmp/中,然后再将index.php放回/public_html/,您认为哪个文件占用的CPU更少
最后一件事,我发现本教程使用PHP移动文件:
但是如何移动目录中的所有文件呢?正则表达式实际上在这方面做得太过分了,因为我们只需要做一些简单的字符串匹配:
$dir = 'the_directory/';
$handle = opendir($dir) or die("Problem opening the directory");
while ($filename = readdir($handle) !== false)
{
//if ($filename != 'index.php' && substr($filename, -3) == '.php')
// I originally thought you only wanted to move php files, but upon
// rereading I think it's not what you really want
// If you don't want to move non-php files, use the line above,
// otherwise the line below
if ($filename != 'index.php')
{
rename($dir . $filename, '/tmp/' . $filename);
}
}
那么问题是:
或者,先将所有文件(包括index.php)移动到/tmp/中,然后再将index.php放回/public_html/,您认为哪个文件占用的CPU更少
这是可以做到的,而且在CPU上可能会稍微容易一些。然而,这并不重要有几个原因。首先,您已经通过PHP以一种非常低效的方式完成了这项工作,因此您不应该真正关注这会给CPU带来的压力,除非您愿意在PHP之外完成这项工作。其次,这将导致更多的磁盘访问(特别是当源目录和目标目录不在同一磁盘或分区上时),并且磁盘访问速度比您的CPU慢得多。正则表达式实际上在这方面做得太多了,因为我们只需要进行一些简单的字符串匹配:
$dir = 'the_directory/';
$handle = opendir($dir) or die("Problem opening the directory");
while ($filename = readdir($handle) !== false)
{
//if ($filename != 'index.php' && substr($filename, -3) == '.php')
// I originally thought you only wanted to move php files, but upon
// rereading I think it's not what you really want
// If you don't want to move non-php files, use the line above,
// otherwise the line below
if ($filename != 'index.php')
{
rename($dir . $filename, '/tmp/' . $filename);
}
}
那么问题是:
或者,先将所有文件(包括index.php)移动到/tmp/中,然后再将index.php放回/public_html/,您认为哪个文件占用的CPU更少
这是可以做到的,而且在CPU上可能会稍微容易一些。然而,这并不重要有几个原因。首先,您已经通过PHP以一种非常低效的方式完成了这项工作,因此您不应该真正关注这会给CPU带来的压力,除非您愿意在PHP之外完成这项工作。其次,这将导致更多的磁盘访问(特别是当源目录和目标目录不在同一磁盘或分区上时),并且磁盘访问速度比您的CPU慢得多。事实上,我进入了第页,要阅读的第一条注释是:
loop through folders and sub folders with option to remove specific files.
<?php
function listFolderFiles($dir,$exclude){
$ffs = scandir($dir);
echo '<ul class="ulli">';
foreach($ffs as $ff){
if(is_array($exclude) and !in_array($ff,$exclude)){
if($ff != '.' && $ff != '..'){
if(!is_dir($dir.'/'.$ff)){
echo '<li><a href="edit_page.php?path='.ltrim($dir.'/'.$ff,'./').'">'.$ff.'</a>';
} else {
echo '<li>'.$ff;
}
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff,$exclude);
echo '</li>';
}
}
}
echo '</ul>';
}
listFolderFiles('.',array('index.php','edit_page.php'));
?>
循环浏览文件夹和子文件夹,并选择删除特定文件。
事实上,我进入了第页,第一条评论是:
loop through folders and sub folders with option to remove specific files.
<?php
function listFolderFiles($dir,$exclude){
$ffs = scandir($dir);
echo '<ul class="ulli">';
foreach($ffs as $ff){
if(is_array($exclude) and !in_array($ff,$exclude)){
if($ff != '.' && $ff != '..'){
if(!is_dir($dir.'/'.$ff)){
echo '<li><a href="edit_page.php?path='.ltrim($dir.'/'.$ff,'./').'">'.$ff.'</a>';
} else {
echo '<li>'.$ff;
}
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff,$exclude);
echo '</li>';
}
}
}
echo '</ul>';
}
listFolderFiles('.',array('index.php','edit_page.php'));
?>
循环浏览文件夹和子文件夹,并选择删除特定文件。
最好的方法是直接通过文件系统来完成,但是如果您必须使用PHP来完成这项工作,那么像这样的操作应该可以满足您的需要—显然,您必须更改路径,使其正确无误。请注意,这假设public_html目录中可能有其他文件,因此它只获取带有4个数字的文件名
$d = dir("public_html");
while (false !== ($entry = $d->read())) {
if($entry == '.' || $entry == '..') continue;
if(preg_match("@^\d{4}$@", basename($entry, ".php")) {
// move the file
rename("public_html/".$entry, "/tmp/".$entry));
}
}
$d->close();
最好的方法是直接通过文件系统来完成,但是如果你一定要用PHP来完成,像这样的东西应该可以做你想做的事情——显然,你必须改变路径,使它们是正确的。请注意,这假设public_html目录中可能有其他文件,因此它只获取带有4个数字的文件名
$d = dir("public_html");
while (false !== ($entry = $d->read())) {
if($entry == '.' || $entry == '..') continue;
if(preg_match("@^\d{4}$@", basename($entry, ".php")) {
// move the file
rename("public_html/".$entry, "/tmp/".$entry));
}
}
$d->close();
您可以将
filesystememitter
与RegexIterator
$source = "FULL PATH TO public_html";
$destination = "FULL PATH TO public_html/tmp";
$di = new FilesystemIterator($source, FilesystemIterator::SKIP_DOTS);
$regex = new RegexIterator($di, '/\d{4}\.php$/i');
foreach ( $regex as $file ) {
rename($file, $destination . DIRECTORY_SEPARATOR . $file->getFileName());
}
您可以将
filesystememitter
与RegexIterator
$source = "FULL PATH TO public_html";
$destination = "FULL PATH TO public_html/tmp";
$di = new FilesystemIterator($source, FilesystemIterator::SKIP_DOTS);
$regex = new RegexIterator($di, '/\d{4}\.php$/i');
foreach ( $regex as $file ) {
rename($file, $destination . DIRECTORY_SEPARATOR . $file->getFileName());
}
你说的BASH问题是什么意思?我不明白…你必须只用PHP来移动它吗?如果是这样的话,你可以使用shell来移动,比如bash。因为我的代码是PHP,我当然想使用PHP来移动它。如果你担心的是CPU消耗,那么,是的,使用bash是更好的选择,在PHP脚本中,你可以使用
proc_open
、system
、exec
等等来完成。除此之外,一如既往:您尝试了什么?您可以通过while(false!==($file=readdir($handle))
在目录中循环,并使用if-else
语句相应地移动BASH问题是什么意思?我不明白…你必须只用PHP来移动它吗?如果是这样的话,你可以使用shell来移动,比如bash。因为我的代码是PHP,我当然想使用PHP来移动它。如果你担心的是CPU消耗,那么,是的,使用bash是更好的选择,在PHP脚本中,你可以使用proc_open
、system
、exec
等等来完成。除此之外,一如既往:您尝试了什么?您可以通过while(false!==($file=readdir($handle))
在目录中循环,并使用if-else
语句相应地移动。我遇到了以下错误:致命错误:在第4行的/home/xxxx/public\u html/test.php中调用未定义的函数open\u dir()。需要移动的文件实际上存储在public_html/web/,我尝试使用$dir='web';和$dir='/web'或$dir='/web/'。它们都会产生相同的错误。我的PHP不支持open_dir函数吗?@RobertHanson:对不起,我错了。这是opendir
,而不是open\u dir
我得到了这个错误:致命错误:在第4行调用/home/xxxx/public\u html/test.php中未定义的函数open\u dir()。需要移动的文件实际上存储在public_html/web/,我尝试使用$dir='web';和$dir='/web'或$dir='/web/'。它们都会产生相同的错误。我的PHP不支持open_dir函数吗?@RobertHanson:对不起,我错了。这是opendir
,而不是open\u dir
bro,您的注册表项:@^\d{4}$@仅用于验证4位数字,对吗?但我刚刚意识到,兄弟,你的注册码:@^\d{4}$@只验证4位数字,对吗?但我才意识到