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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献