简体   繁体   中英

read files from an array in 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>";    
                        }
                    }
                }
            }

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.

Instead of iterating the directory with readdir , consider using glob() instead. It allows you to specify a pattern and it returns all files that match it.

Secondly, your while loop has an error: you conditionally add the file name to the list of files, but then you always print every file name using a foreach loop. On the first loop it will print the first file. On the second loop it will print the first and second files, etc. You should separate your while and foreach loops to fix that issue (ie unnest them).

Using glob, the modified code will look like:

$file_list = glob('/path/to/files/*.txt');

foreach ($file_list as $file_name) {
    if (is_file($file_name) && is_writable($file_name)) {
        // Do something with $file_name
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM