简体   繁体   中英

How to get images from image folder on real time server?

I have an Apache Server running on localhost and an images folder inside the document root. In order to get all the images inside the images folder i'm using the below code

if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
    echo "<pre>";
        echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";

    $path = "/images/$file";
    echo "<img src=".$path." alt='image' width='50px' height='50px'>";
    echo "</pre>";
    }
      closedir($dh);
  }
}
?>

In real time my problem is that I have a domain https://www.example.com/ where an images folder is kept from where i need to get all the images, I'm able to view the images one by one by giving the url as https://www.example.com/euf/assets/images/background.jpg in the browser, that means euf folder should be kept under the doument root of the server so if I give path as $dir=$_SERVER['DOCUMENT_ROOT']."euf/assets/images/" the above code should work .... But the code is not working in that case. Please help

Try this

    <?php
    $dir = $_SERVER['DOCUMENT_ROOT']."your/imagesfolder/path/";
    $dh = opendir($dir);
    while (($file = readdir($dh)) !== false)
    {   

        echo "<pre>";
        echo "filename:". $file." : filetype: " . filetype($file) . "\n";

        $path = "/images/".$file;
        echo "<img src=" . $path . " alt='image' width='50px' height='50px'>";
        echo "</pre>";
    }
    closedir($dh);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM