【问题标题】:PHP read content from file errorPHP从文件错误中读取内容
【发布时间】:2014-11-01 12:59:56
【问题描述】:

所以我需要先打开一个目录,然后我需要创建一个名为“.”的文件,并且它可以工作。并且“..”不显示,我这样做了,它也可以正常工作,但毕竟我需要打开该目录中的每个文件(除了那个“.”和“..”)并显示它的内容。

我的代码在这里:

<?php
    $handle = opendir('data');
    $files = array();
    while (false !== ($file = readdir($handle))) {
        if ($file!=="." && $file!=="..") {
            $files = $file;
            print_r ('<p>' . ucfirst($files) .'</p>');
        }
        foreach($files as $dataz) {
            $handle2 = fopen('data/'.$dataz, 'r');
            while (!feof($handle2)) {
                $name = fgets($handle2);
                echo '<p>' . $name .'</p>';
            }
            fclose($handle2);
        }

    }
    closedir($handle);
?>

我得到的错误是: 警告:在第 30 行的 /home/something/something/websitephp/weather.php 中为 foreach() 提供的参数无效调用堆栈:0.0025 325952 1. {main}() /home/something/something/websitephp/weather.php: 0

我认为错误将是 $dataz 但我需要它以便向 fopen 指示应该打开哪些文件。

【问题讨论】:

    标签: php file directory fgets


    【解决方案1】:

    这里

    $files = $file;
    

    你每次都写完了数组

    使用

    $files[] = $file;
    

    改为

    编辑

    $handle = opendir('data');
    $files = array();
    while (false !== ($file = readdir($handle))) {
        if ($file !== "." && $file !== "..") {
            $files[] = $file;
            print_r('<p>' . ucfirst($files) . '</p>');
        }
    }
    
    foreach ($files as $dataz) {
        $handle2 = fopen('data/' . $dataz, 'r');
        while (!feof($handle2)) {
            $name = fgets($handle2);
            echo '<p>' . $name . '</p>';
        }
        fclose($handle2);
    }
    closedir($handle);
    

    【讨论】:

    • 这很好,因为它允许我显示文件中的内容,但现在文件名不再是“温度”或“天气”而是“数组”。知道为什么吗?谢谢!
    • 我也确实解决了这个问题,但我仍然有问题。第一个文件被正确读取和显示,但是当它显示第二个文件时,它首先再次显示第一个文件的内容,然后显示第二个文件的内容。第三个文件也是如此,首先显示前两个文件的内容,然后显示第三个文件的内容。
    • fgets 从文件中获取一行日期,而不是其名称,如果您想要文件名列表,请使用 glob
    • 哦,是的,我看到了你需要将 foreach 循环移到第一个 while 循环之外的问题,然后再放置
    • 如果我这样做,它首先显示文件名,全部 3 个,然后显示所有文件的内容。为了向您展示我想要的外观示例,这将是 NameOfFile,下一行是 NameOfFile 的内容,在此 NameOfFile2 之后,下一行是 NameOfFiles2 的内容等等。抱歉,不是很具体.
    【解决方案2】:

    我重新组织了代码并使其工作

    $directoryPath = 'data';
    
    // Get the file listing
    $files = array();
    foreach (scandir($directoryPath) as $file) {
        if (is_file("$directoryPath/$file")) {
            $files[] = $file;            
        }
    }
    
    // Display each files content
    foreach($files as $file) {
        echo '<p>' . ucfirst($file) .'</p>';
    
        $contents = file_get_contents("$directoryPath/$file");
    
        // Print each line of the file
        foreach (explode("\n", $contents) as $line) {
            echo '<p>' . $line .'</p>';            
        }
    }
    

    【讨论】:

    • 这很好,但现在每个文件都在&lt;p&gt;&lt;/p&gt; 标签中,我想他想要每一行
    • @andrew Aah 你是对的,错过了原始代码中的 fgets
    【解决方案3】:

    所以,我目前的解决方案是:

    <?php
        $handle = opendir('data');
        $files = array();
        while (false !== ($file = readdir($handle))) {
            if ($file!=="." && $file!=="..") {
                $files[] = $file;
                print_r ('<p>' . strtoupper($file) .'</p>');
            }
            foreach($files as $dataz) {
                $handle1 = fopen('data/'.$dataz, 'r');
                while (!feof($handle1)) {
                    $name = fgets($handle1);
                    echo '<p>' . $name .'</p>';
                }
                fclose($handle1);
            }
        }
        closedir($handle);
    ?>
    

    它得到的是除了“.”之外的文件名。和“..”并读取文件的内容,但是当它显示第二个文件的内容时,首先显示第一个文件的内容,然后显示第二个文件的内容,即应该显示。出于某种原因,$name 似乎保留了前一个文件的内容。

    要不明白我在说什么,请看一下这个link

    【讨论】:

    • 更新您的原始帖子
    猜你喜欢
    • 2013-03-02
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2019-07-07
    相关资源
    最近更新 更多