Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中循环目录?_Php_File_Directory - Fatal编程技术网

在PHP中循环目录?

在PHP中循环目录?,php,file,directory,Php,File,Directory,我正在编写一个脚本,用一个字符串替换目录和子目录中的所有文件,以消除软件显示的一般错误消息 我使它在一个目录中的所有文件都可以很容易地工作,但是我们在一个包含子目录的文件夹中运行它,正如您可能猜到的,它抛出了很多错误。我完全忘记了子目录 所以现在我正在制作一个与子目录一起工作的脚本,但是我被难住了 这是我的密码: <?php $files = explode("\n", shell_exec('ls')); $count = 0; foreach ($files as $file) {

我正在编写一个脚本,用一个字符串替换目录和子目录中的所有文件,以消除软件显示的一般错误消息

我使它在一个目录中的所有文件都可以很容易地工作,但是我们在一个包含子目录的文件夹中运行它,正如您可能猜到的,它抛出了很多错误。我完全忘记了子目录

所以现在我正在制作一个与子目录一起工作的脚本,但是我被难住了

这是我的密码:

<?php

$files = explode("\n", shell_exec('ls'));
$count = 0;

foreach ($files as $file)
{
    if (empty($file) || $file == $_SERVER['SCRIPT_NAME'])
    {
        continue;
    }
    if (is_dir($file))
    {
        echo "Copying to {$file}/{$_SERVER['SCRIPT_NAME']}\n";
        copy($_SERVER['SCRIPT_NAME'], $file . "/" . $_SERVER['SCRIPT_NAME']);
        exec("php {$file}/{$_SERVER['SCRIPT_NAME']}");
        unlink($file . "/" . $_SERVER['SCRIPT_NAME']);
        continue;
    }
    $fh = fopen($file, 'w');
    fwrite($fh, '<!-- Generated %T by %h (%s) -->');
    fclose($fh);
    echo "Rewrote {$file}\n";
    $count++;
}
echo "Finished. Rewrote {$count} files. Don't forget to delete {$_SERVER['SCRIPT_NAME']}.\n";

?>

尝试以下方法:

或者尝试在google上搜索“php递归目录列表”或任何类似内容


使用readdir()函数,您将获得特定目录中的文件和子目录。所以关键是要弄清楚它是否只是一个文件,或者它是一个子目录,它具有is_udir()函数。另外,要打开和编辑子目录中的文件时,请确保使用该文件的完整路径。就像我说的,试着用谷歌找到有用的东西

服务器['SCRIPT\u NAME']
的值可能不是您所期望的,这也是构建类似
test/test/p.php的路径的原因。我的猜测是,当您执行
php测试/p.php
时,这就是php放入
SCRIPT\u NAME
的值。您可以使用()函数来解决这个问题

另外,在动态创建shell命令时,应该使用()

或者

$self = realpath(__FILE__);
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
foreach ($ritit as $splFileInfo) {
    $fileName = $splFileInfo->getRealPath();
    if ($fileName !== $self) {
        file_put_contents($fileName, '<!.....stuff');
    }
}
$self=realpath(文件);
$ritit=new recursive迭代器(new recursivedirectoryinterator(_DIR__));
foreach($splFileInfo){
$fileName=$splFileInfo->getRealPath();
如果($fileName!=$self){

文件内容($fileName,问题源于LS返回基本目录的内容,不一定是包含脚本的目录。老实说,我会避免执行,而只是尝试将复制抽象为函数,使用php文件系统函数,并将其转换为递归算法,而不是尝试执行LS进程。

SPL的递归ive迭代器非常适合这种情况,我将使用如下内容

<?php
// get scriptname from $argv[0]
$scriptname = basename(array_shift($argv));

// optional argument indicating path to run this script in (defaults to current path)
if (!empty($argv[0])) {  
    $workspace = $argv[0];  
}else {  
    $workspace = getcwd();  
}

// Recursively iterate over files in the specified workspace folder and all subfolders  
try {  
    $count = 0;  
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($workspace)) as $file) {  
        // ignore the folders, and this script if present   
        if ($file->isDir() || $file->getFilename() == $scriptname) continue;  
        // write the file  
        echo "Writing to $file\n";  
        file_put_contents($file, '<!-- Generated %T by %h (%s) -->');  
        $count++;  
    }  
    echo "Rewrote $count files";  
} catch (Exception $e) {  
    // oops, likely invalid path, or unreadable folder   
    echo "Problem reading $workspace";  
}  
?>  

SPL手册:



有什么理由不使用
sed
或其他更简单的linux实用程序吗?@tandu,老实说,我不知道
sed
是什么。除了我只是一名PHP程序员之外,没有理由不使用。所以我选择了PHP。我对其他方面了解不多。它仍然很有用。谢谢你的代码,但我想为它编写自己的代码。开始吧g完成的任务当然是首要任务,但我也想自己学习。我将在大约40分钟后到家,届时我将根据您的basename()建议发布我的新代码。
<?php
// get scriptname from $argv[0]
$scriptname = basename(array_shift($argv));

// optional argument indicating path to run this script in (defaults to current path)
if (!empty($argv[0])) {  
    $workspace = $argv[0];  
}else {  
    $workspace = getcwd();  
}

// Recursively iterate over files in the specified workspace folder and all subfolders  
try {  
    $count = 0;  
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($workspace)) as $file) {  
        // ignore the folders, and this script if present   
        if ($file->isDir() || $file->getFilename() == $scriptname) continue;  
        // write the file  
        echo "Writing to $file\n";  
        file_put_contents($file, '<!-- Generated %T by %h (%s) -->');  
        $count++;  
    }  
    echo "Rewrote $count files";  
} catch (Exception $e) {  
    // oops, likely invalid path, or unreadable folder   
    echo "Problem reading $workspace";  
}  
?>