how to echo directory name [closed]

穿精又带淫゛_ 提交于 2019-12-11 04:41:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!