【发布时间】:2017-03-16 15:13:04
【问题描述】:
我正在尝试显示存储在 Web 根目录之外的用户图像(出于安全目的)。我已经尝试过提出here 的建议,但是当我导航到show_imgs.php 时,我的浏览器返回一堆乱码,似乎进入了无限循环。
我的代码与示例的唯一不同之处在于我的代码处于循环状态。不确定这是否与它有关。
我的代码:
$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 类型验证(如果我应该做更多安全检查,请说出来)。
【问题讨论】: