Reputation: 25
How do I display all images in a folder on my page?
HTML
<div class="itemS">
<li><div class="itemType"><input type="image" src="image/blah1.jpg"/><gt_descA>Description here</gt_descA></div></li>
</div>
I want to use php to loop thru the images and display all of them along with the file name. But it doesn't work.
<?php
$folder = 'blah/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
echo "<div class="itemS">
<li><div class="itemType"><input type="image" src="'.$files[$i].'"/><gt_descA>"substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder))"</gt_descA></div></li>
</div>";
}
?>
Upvotes: 2
Views: 1058
Reputation: 231
changes in you code is as follows :
hi i write this code for you and its working ...
you don't need the one more $count variable to be define.
<?php
$folder = 'images/';
$filetype = '*.*';
$div= '';
foreach (glob($folder.$filetype) as $files) {
$div .= '<div class="itemS">';
$div .= '<li><div class="itemType"><input type="image" src="'.$files.'"/><gt_descA>"'.substr($files,strlen($folder),strpos($files, '.') - strlen($folder)).'"</gt_descA></div></li>';
$div .= '</div>';
}
echo $div;
?>
Upvotes: 0
Reputation: 289
here problem is quotes positions single and double.. try this code
$folder = 'blah/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
echo "<div class='itemS'>
<li><div class='itemType'><input type='image' src='".$files[$i]."'/> <gt_descA>".substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder))."</gt_descA></div></li>
</div>";
}
Upvotes: 1
Reputation: 3
I do believe that there is a need to concatenate the string in ECHO for php
try the below
<?php
$folder = 'blah/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
for ($i = 0; $i < $count; $i++) {
echo "<div class="itemS">
<li><div class="itemType"><input type="image" src="'.$files[$i].'"/><gt_descA>".substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder))."</gt_descA></div></li>
</div>";
}
?>
Upvotes: 0