doujinai2183 2016-03-14 13:54
浏览 29

从php中的数组中读取文件

I'm trying to open a directory, read just files with a .txt format and then display the contents. I've coded it out, but it doesn't do anything, although it doesn't register any errors either. Any help?

$dir = 'information';
    If (is_dir($dir)) {
    $handle = opendir($dir);
    } else {
    echo "<p>There is a system error</p>";
}
$entry=array();
        while(false!==($file = readdir($handle))) {
            if ( !strcmp($file, ".") || !strcmp($file, "..")) {
                }
                else if(substr($file, -4) == '.txt') {
                    $entry[] = $file;
                    }
            foreach ($entry as $txt_file) {
                    if(is_file($txt_file) && is_writable($txt_file))   {
                        $file_open = fopen($txt_file, 'r');
                        while (!feof($file_open)) {
                            echo"<p>$file_open</p>";    
                        }
                    }
                }
            }
  • 写回答

2条回答 默认 最新

  • 普通网友 2016-03-14 16:10
    关注

    Help is quite simple.

    Instead

    $dir = 'information';
    If (is_dir($dir)) {
    $handle = opendir($dir);
    } else {
    echo "<p>There is a system error</p>";
    }
    

    write (I am sorry for re-formatting of new lines)

    $dir = 'information';
    if(is_dir($dir))
    {
        $handle = opendir($dir);
    }
    else
    {
        echo "<p>There is a system error</p>";
    }
    

    because if has to be written only smallcaps, thus not If.

    And the second part rewrite to (again, you may use your own formatting of new lines)

    $entry=array();
    $file = readdir($handle);
    while($file !== false)
    {
        if(!strcmp($file, ".") || !strcmp($file, ".."))
        {
        }
        elseif(substr($file, -4) == '.txt')
        {
            $entry[] = $file;
        }
    
        foreach ($entry as $txt_file)
        {
            if(is_file($txt_file) && is_writable($txt_file))
            {
                $file_open = fopen($txt_file, 'r');
                while(!feof($file_open))
                {
                    echo"<p>$file_open</p>";    
                }
            }
        }
    }
    

    because PHP has elseif, not else if like JavaScript. Also I separated $file = readdir($handle) for possible source of error.

    Code part

    if(!strcmp($file, ".") || !strcmp($file, ".."))
    {
    }
    elseif(substr($file, -4) == '.txt')
    {
        $entry[] = $file;
    }
    

    should be shortened only to

    if(substr($file, -4) == '.txt')
    {
        $entry[] = $file;
    }
    

    because when if part is empty, then it is not neccessary.

    That is all I can do for you at this time.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!