0
我试图显示存储在Web根目录之外的用户图像(出于安全目的)。我已经尝试了here提出的建议,但是当我导航到show_imgs.php
时,我的浏览器返回了一堆乱码符号,似乎进入了无限循环。显示来自web根目录以外的图像无效?
唯一不同的关于我的代码与例子是我的代码是在一个循环中。不知道这与它有什么关系。
我的代码:
$image_array = ["img_1.png", "img_2.png", "img_3.png"];
$i = 1;
foreach($image_array as $image) {
//Specify absolute path
$image_path = "/absolute/path/to/img_$i.png";
//Get file contents. I have also tried readfile() in place of this.
$contents = file_get_contents($image_path);
//Perhaps this is causing the infinite loop?
header('Content-type: image/png');
//Display contents of file (aka the image)
echo $contents;
$i++;
}
当然,我会想用file_get_contents
安全之前增加文件大小和MIME类型的验证(如果有更多的安全检查,我应该做的请告诉我)。
所以你说我应该在images.php做这些操作,然后调用display_the_images图像.PHP?这是否必须在URL中完成,然后使用GET,还是只能在display_the_images.php的PHP脚本中完成? – faceymcface
所以只有一个文件(例子中的images.php,但你可以任意命名)有php,它将“读取和读出”doc根目录以外的图像。然后在你通常放置图像的html文件中...你把这个带有GET var的php文件放在它上面让image.php知道要去哪个图像。 – WEBjuju
这工作!谢谢你,先生 – faceymcface