首页 > 解决方案 > 使用 php 显示来自 ftp 服务器的图像

问题描述

如何使用 php 在网站中显示来自 ftp 服务器的所有图像?它也应该自动排序,所以我可以将更多图像添加到 ftp 服务器,它会工作。

我正在尝试制作画廊类型的东西

我已经浏览了谷歌并没有发现任何有用的东西。我还在学习php,对它了解不多。

任何帮助将非常感激。谢谢!

标签: phphtml

解决方案


正如已经评论的scandir那样,成功了。下面的代码获取所有图像并将它们作为<img>元素输出。

index.php

// Here enter the base url of the images folder 
$base_images_url = "http://example.com/images/";

$dir = dirname (__FILE__) . "/images/";

// Get the files in the folder. Sort in ascending order - this is the default
$images = scandir($dir);

// Or is you need to sort in descending order
//$images = scandir($dir,1);

// Output an img tag for every image.
foreach($images as $image){
    // skip the . and .. that starts the folder listing
    if($image === '.' || $image === '..'){
        continue;
    }
    $image_url = $base_images_url . $image;
    echo '<img src="' . $image_url . '">';
}

这适用于以下文件夹设置:

-index.php
-images
   -image1.png
   -anotherimage.jpg

推荐阅读