PHP 如何将每个子文件夹中的图像和文本文件作为 table

PHP how to echo an image and text file from each subfolders as a table

我是 PHP 的新手,不是高级 Web 开发人员,但我真的很喜欢学习。我的问题如下:我想制作一个项目页面,显示位于子文件夹中的每个项目。很明显,该页面将包含每个文件夹的缩略图和两行描述(在文本文件中),以将其显示为 table,用户可以单击该页面以访问正确的项目页面。这是我目前坚持使用的代码:

<?php      
$dir = "*/";
$images = glob($dir."main.jpg" );
$myFiles = glob($dir."description.txt");
$fh = fopen($myFiles, 'r');
$theData = fread($fh, 5);
fclose($fh);

foreach( $images as $image ):
echo '<div class="projects"><div class="projects-img-container">';
echo" <img class='projet-img' src='". '/projects/' . $image . "'/>";
echo '</div><div class="project-description">';
foreach( $myfiles as $myfile ):
echo $theData;
endforeach;
echo '</div>';
endforeach;
echo '</div>';
?>

description.txt如下:

Gold Collection
Deck 1

任何帮助将不胜感激,因为目前图像显示正常,但没有显示文本。然后也会将 link 到相应的文件夹...

应该看起来像这张图片: http://i.stack.imgur.com/yjwX7.jpg

感谢您的帮助!

查看下面修改后的脚本中的评论和代码以获取建议以及我实施这些建议的方式...

<?php      
$dir = "*/";
$images = glob($dir."main.jpg" );
/* This doesn't work because fopen() can't take an array
* $myFiles = glob($dir."description.txt");
* $fh = fopen($myFiles, 'r');
* $theData = fread($fh, 5);
* fclose($fh);
*/
echo '<div class="projects">'; // I think you meant to put this outside the loop
foreach( $images as $image ) {
   echo '<div class="projects-img-container">';
   echo" <img class='projet-img' src='". '/projects/' . $image . "'/>";
   echo '</div><div class="project-description">';
   $dn = dirname($image);
   # $myFiles = glob($dn."/description.txt"); // don't think we need to glob() this
   $fh = fopen($dn."/description.txt", 'r');
   $theData = fread($fh, 5);
   echo $theData;
   fclose($fh);
   /* Not sure it makes sense to loop $myfiles - only 1 description.txt can
    * exist in any one directory, right?
    * foreach( $myfiles as $myfile ):
    *  echo $theData;
    *}
    */
   # Whatever you want to do with the directory link would be done with $dn here.
   echo '</div>';
}
echo '</div>';
?>

经过审查,这似乎对我很有效。谢谢你彼得!

<?php      
$dir = "*/";
$images = glob($dir."main.jpg" );

echo '<div class="projects-container">'; 
foreach( $images as $image ) {

$dn = dirname($image);

echo '<div class="projects">';
echo '<div class="projects-img-container">';
echo" <a href='".$dn."'><img class='projet-img' src='". '/projects/' . $image . "'/></a>";
echo '</div><div class="project-description">';

$fh = fopen($dn."/description.txt", 'r');
$theData = fread($fh, 5);
echo" <a href='".$dn."'>".$theData."</a>";
fclose($fh);
echo '</div>';
echo '<div style="clear: both"></div>';
echo "</a>";
echo '</div>';
}
echo '</div>';
?>