dtpoius74857 2018-10-18 19:52
浏览 43
已采纳

无法使用PHP foreach循环成功遍历目录中的imag

Here's the loop:

<?php
// I added this just to see if HTML_PATH_UPLOADS_PAGES points to the right directory
echo "<img style=\"width:100%;height:100%;\" src=\"" . HTML_PATH_UPLOADS_PAGES . "c60fca4c682803c969f2410084878155/1.jpg\">
";
// Loop through all the image files in the right sidebar.
foreach (glob(HTML_PATH_UPLOADS_PAGES . "c60fca4c682803c969f2410084878155/{*.[gG][iI][fF],*.[jJ][pP][gG],*.[jJ][pP][eE][gG],*.[pP][nN][gG]}", GLOB_BRACE) as $image) {

    // Display the image in the right sidebar.
    echo "<div class=\"col-md-6 col-lg-4 item zoom-on-hover\">
";
    echo "<a class=\"lightbox\" href=\"" . HTML_PATH_UPLOADS_PAGES . "c60fca4c682803c969f2410084878155/" . basename($image) . "\">
";
    echo "<img class=\"img-fluid image\" src=\"" . HTML_PATH_UPLOADS_PAGES . "c60fca4c682803c969f2410084878155/" . basename($image) . "\" >
";
    echo "</a>
";
    echo "</div>
";
}

?> 

Here's the location of the pictures.php file where the loop is placed: location of the pictures.php file where I placed the foreach loop

Here's the directory where the images are: images I want to loop through

Edit:

Here's the definition of HTML_PATH_UPLOADS_PAGES as defined in /bludit/bl-kernel/boot/init.php:

$base = '';
if (!empty($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['SCRIPT_NAME']) && empty($base)) {
    $base = str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_NAME']);
    $base = dirname($base);
} elseif (empty($base)) {
    $base = empty( $_SERVER['SCRIPT_NAME'] ) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $base = dirname($base);
}
if (strpos($_SERVER['REQUEST_URI'], $base)!==0) {
    $base = '/';
} elseif ($base!=DS) {
    $base = trim($base, '/');
    $base = '/'.$base.'/';
} else {
    // Workaround for Windows Web Servers
    $base = '/';
} 

define('HTML_PATH_ROOT', $base);
define('HTML_PATH_UPLOADS_PAGES',        HTML_PATH_ROOT.'bl-content/uploads/pages/');
  • 写回答

1条回答 默认 最新

  • dsen53898 2018-10-18 23:10
    关注

    Try adding this constant to get the filesystem path of your folder:

    define('FILE_PATH_UPLOADS_PAGES', 
      rtrim($_SERVER['DOCUMENT_ROOT'], '/\\') . '/bludit/bl-content/uploads/pages/');
    

    Then use it with glob:

    glob(FILE_PATH_UPLOADS_PAGES . 'c60fca4c682803c969f2410084878155 ...');
    

    Alternative with a relative path:

    define('FILE_PATH_UPLOADS_PAGES', 
      __DIR__ . '../../bl-content/uploads/pages/');
    

    However, if you really don't want to create another constant at all, you can try:

    glob(rtrim($_SERVER['DOCUMENT_ROOT'], '/\\') 
      . '/' . HTML_PATH_UPLOADS_PAGES 
      .  'c60fca4c682803c969f2410084878155...');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?