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

无法使用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
    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...');
    
    点赞 评论

相关推荐