dtpoius74857 2018-10-18 11: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 15: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...');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部