I have a piece of php code that lists directories and then creates div's with information regarding the directories inside of it. However when I do $newsPieces[i] it doesn't include the name of the folder in the string, why?
<?php
$newsPieces = scandir("posts");
for ($i = 2; $i < count($newsPieces) and $i < 10; $i++) {
$titlefile = fopen($newsPieces[i] . "/title.txt", "r");
$textfile = fopen($newsPieces[i] . "/text.txt", "r");
$fullElement = "<div class='newsPiece'><div class='newsImageHolder'><img class='newsImage' src='" . $newsPieces[i] . "/image.jpg'></div><div class='newsContent'><h1 class='newsTitle'>" . fread($titlefile, filesize($newsPieces[i] . "/title.txt")) . "</h1><p class='newsText'>" . $piece[] = fread($textfile, filesize($newsPieces[i] . "/text.txt")) . "</p><a class='newsLink' href='post?post=" . urlencode($newsPieces[i]) . "'>Continue Reading</a></div></div>";
echo $fullElement;
fclose($titlefile);
fclose($textfile);
}
?>
Fixed Version:
<?php
$newsPieces = scandir("posts");
for ($i = 2; $i < count($newsPieces) and $i < 12; $i++) {
$titlefile = fopen("posts/" . $newsPieces[$i] . "/title.txt", "r") or die("Unable to open file!");
$textfile = fopen("posts/" . $newsPieces[$i] . "/text.txt", "r") or die("Unable to open file!");
$fullElement = "<div class='newsPiece'><div class='newsImageHolder'><img class='newsImage' src='posts/" . $newsPieces[$i] . "/image.jpg'></div><div class='newsContent'><h1 class='newsTitle'>" . fread($titlefile, filesize("posts/" . $newsPieces[$i] . "/title.txt")) . "</h1><p class='newsText'>" . fread($textfile, filesize("posts/" . $newsPieces[$i] . "/text.txt")) . "</p><a class='newsLink' href='post?post=" . urlencode($newsPieces[$i]) . "'>Continue Reading</a></div></div>";
echo $fullElement;
fclose($titlefile);
fclose($textfile);
}
?>