我不确定此脚本的确切位置,因此它必须能够从任何地方访问此目录。
我尝试通过从目录中获取图像文件名来创建图像列表,过滤它们直到我只有我想要的图像格式,然后使用<img>
标记显示它们。
第一位进展顺利。输出HTML证明是一个问题。
虽然我可以使用$_SERVER["DOCUMENT_ROOT"] to work with the directories in PHP, it's problematic to output that value as part of the path in the
tag's
src`属性。
这是我目前的代码:
$unkwown_files = scandir($_SERVER['DOCUMENT_ROOT'] . "/path/images");
foreach($unkwown_files as $file) {
$exploded_filename = explode(".", $file);
$file_type = array_pop($exploded_filename);
$accepted_filetypes = [
"png",
"jpg",
"gif",
"jpeg"
];
$picture_names = [];
if (in_array($file_type, $accepted_filetypes)) {
$picture_names[] = $file;
}
}
foreach($picture_names as $picture) {
$path_to_image = $_SERVER['DOCUMENT_ROOT'] . "/nodes/images" . $picture;
echo '<img src="' . $path_to_image . '" class="upload_thumbnail"/>';
}
答案 0 :(得分:1)
一种略有不同的方法,从一开始就按扩展名过滤文件。
System.Data.SqlServerCe.dll
答案 1 :(得分:0)
试试这个
foreach($picture_names as $picture) {
$path_to_image = $_SERVER['DOCUMENT_ROOT'] . "/nodes/images/" . $picture;
echo '<img src="' . $path_to_image . '" class="upload_thumbnail"/>';
}