问题
how to echo directory name with images gallery
this code showing random images and i want to echo directory name with images please help me to fix this issue.
**i want like this
example
______________
| this |
| is |
| image |
| |
|_____________|
Directory Name
**
<?php
$directory = "./*/";
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }
//shuffle array
shuffle($imgs);
//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 20);
?>
<table id="results">
<tr>
</tr>
<tr>
<?php $num_of_files = 0; $i=0; foreach ($imgs as $img):?>
<td><a href="<?php echo $img ?>" target="_blank"><img class="shadow1" name="field-name" src="<?php echo $img ?>thumb.jpeg" width=165 height=135 /></td>
<?php if (++$num_of_files == 12)break; ?>
<?php if(++$i%3==0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>
回答1:
Why don't you just echo $directory
below the <img>
tag?
<tr>
<?php foreach ($imgs as $img): ?>
<td>
<a href="<?php echo $img ?>" target="_blank">
<img class="shadow1" name="field-name" src="<?php echo $img ?>thumb.jpeg" width=165 height=135 />
</a>
<p><?php echo basename($directory); ?></p>
</td>
<?php endforeach; ?>
</tr>
Do you not have access to this parameter in your current scope? Another option is:
<p><?php echo basename(pathinfo($img, PATHINFO_DIRNAME)); ?></p>
回答2:
echo basename(realpath('./'));
realpath:
realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and return the canonicalized absolute pathname.
and
basename:
Given a string containing the path to a file or directory, this function will return the trailing name component.
Refer basename and realpath on Official site.
来源:https://stackoverflow.com/questions/14911685/how-to-echo-directory-name