php - 使用 php 显示来自 ftp 服务器的图像
问题描述
如何使用 php 在网站中显示来自 ftp 服务器的所有图像?它也应该自动排序,所以我可以将更多图像添加到 ftp 服务器,它会工作。
我正在尝试制作画廊类型的东西
我已经浏览了谷歌并没有发现任何有用的东西。我还在学习php,对它了解不多。
任何帮助将非常感激。谢谢!
解决方案
正如已经评论的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
推荐阅读
- python - Python - 访问字典中的所有属性
- azure-devops - Azure DevOps API 源管理 - 设置源权限不起作用
- azure-sql-database - Azure 逻辑应用内部服务器错误 500
- mysql - 学习Inner join multitable时发现SQL查询报错
- google-maps - 为什么 turfjs 距离返回的结果与 Google Map 几何库 computeDistanceBetween 不同?
- jenkins - 无法将参数从一个管道作业传递到另一个
- reactjs - 如何更改 React 地图中的渲染顺序
- tfs - 功能的 TFS 查询未“完成”,但所有子用户故事都“完成”
- javascript - 如果 Vimeo 视频未加载,则创建静态图像后备
- c# - 如何使用 Postman 将 JSON 数组发布到 ASP.Net Core 应用程序