如何在单个页面中提供位于www根之上的多个图像?

Dre*_*rew 5 php upload symlink image-gallery

我希望为用户提供用户提交的图片库.我编写了一个上传脚本,将文件保存在www根目录之上.我知道我可以通过指定页眉然后使用readfile来提供文件,但是我打算将图像放在一个表中以显示其他信息,并且不要认为header/readfile是最好的解决方案.我想也许是符号链接,但我不确定.

实现这一目标的最佳方法是什么?

Mic*_*ski 5

你需要一个脚本getimage.php来发送图像标题并回显它的内容.然后在HTML中,您只需将其用作<img src=''>HTML中的.该唯一的目的getimage.php就是获取和输出的图像.它与用于生成发送到浏览器的HTML的PHP​​无关.

此外,您可以检查用户是否具有有效的会话和查看图像的权限getimage.php,如果没有,则发送某种访问被拒绝的图像.

内容getimage.php小而简单:

// Check user permissions if necessary...

// Retrieve your image from $_GET['imgId'] however appropriate to your file structure
// Whatever is necessary to determine the image file path from the imgId parameter.

// Output the image.
$img = file_get_contents("/path/to/image.jpg");
header("Content-type: image/jpeg");
echo($img);
exit();
Run Code Online (Sandbox Code Playgroud)

在您的HTML中:

<!-- as many as you need -->
<img src='getimage.php?imgId=12345' />
<img src='getimage.php?imgId=23456' />
<img src='getimage.php?imgId=34567' />
Run Code Online (Sandbox Code Playgroud)

然后它成为浏览器的工作,getimage.php?imgId=12345作为图像的路径.浏览器不知道它正在调用PHP脚本,而不是Web可访问目录中的图像.