0

ディレクトリ内のすべてのファイルを表示してから、fgets を使用してそれらの横にいくつかの情報を表示しようとしています。私はこれを機能させることができないようです。現時点では、php が苦手ですが、基本的には、tr ごとに 2 つの td を持つ tr を表示したいと考えています。1 つはファイル名の td で、もう 1 つは「説明」の td です。これは、ファイルの最初の部分に入力した情報です。

<table>
<?php
if ($handle = opendir('desktop/')) {
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {

      $string = "$file";     


$searchArray = array("_", ".php");
$replaceArray = array("", "");
$string = str_replace($searchArray,$replaceArray,$string);
$handle = @fopen("desktop/$file", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
    echo $buffer;
}
if (!feof($handle)) {
    echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
echo "<tr><td><a href=desktop/$file>$string\n</a></td><td>$buffer</td></tr>"; 
    }
}
closedir($handle);
}
?>
</table>
4

1 に答える 1

0

You redefine "$handle" which casues your problem.

$handle is initially the variable holding the "directory" resource. You then re-use it to hold the file resource. Then when you return back the the directory resource to get the name of the next file, $handle is no longer the directory resource, but a file - so problems arise.

Call the first one "$dirHandle" and the second "$fileHandle" as this will make it clearer for you?


Edit: how to only get the first line (as requested in comments):

if ($handle) { 
    if (($buffer = fgets($handle, 4096)) !== false) { 
        echo "<tr><td><a href=desktop/$file>$string\n</a></td><td>$buffer</td></tr>";  
    }
} 

(Note: you probably also want to use htmlspecialchars() to make it a bit safe too)

于 2012-07-18T02:46:51.703 に答える