如何在单个页面中提供位于www根目录上方的多个图像? [英] How to serve multiple images which reside above the www root within a single page?

查看:105
本文介绍了如何在单个页面中提供位于www根目录上方的多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am hoping to offer users a user submitted image gallery. I have written a upload script which saves the file above the www root. I know I can serve the file by specifying the page header and then using readfile, however I am planning to throw the images within a table to be displayed with other information and dont think the header/readfile is the best solution. I am thinking maybe symlinks but I am not sure.

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

What is the best method to achieve this?

推荐答案

您将需要类似getimage.php的脚本,该脚本发送图像标题并回显其内容.然后在HTML中,只需将其用作HTML中的<img src=''>即可. getimage.php only 目的是检索和输出图像.它与用于生成发送到浏览器的HTML的任何PHP都是分开的.

You'll need a script like getimage.php which sends the image headers and echo's out its contents. Then in your HTML, you just utilize it as the <img src=''> in your HTML. The only purpose of getimage.php is to retrieve and output the image. It remains separate from whatever PHP you use to generate the HTML sent to the browser.

此外,您可以检查用户是否具有有效的会话并具有在getimage.php中查看图像的权限,否则,请发送某种拒绝访问的图像.

Additionally, you can check if the user has a valid session and permission to view the image in getimage.php and if not, send a some kind of access-denied image instead.

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();

在您的HTML中:

<!-- as many as you need -->
<img src='getimage.php?imgId=12345' />
<img src='getimage.php?imgId=23456' />
<img src='getimage.php?imgId=34567' />

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

It then becomes the browser's job to call getimage.php?imgId=12345 as the path to the image. The browser has no idea it is calling a PHP script, rather than an image in a web accessible directory.

这篇关于如何在单个页面中提供位于www根目录上方的多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆