我不想显示图片的真实网址。因此,我使用uniqueid()
替换动态图片的网址,并将真实网址存储在会话中的uniqueid()
中。所以我会在会话数组中加载uniqueids => real-image-path.jpg
。然后使用以下代码回显图像:
<img src="getImage2.php?h=<?php echo $uniqueID; ?>" title="" >
这一切似乎都有效。在getImage2.php
中,我想知道如何使用存储在会话中的变量来回显图像。我有:
session_start();
header("Content-Type: image/jpeg");
while($_SESSION[$uniqueID]){
echo readfile($_SESSION[$uniqueID]);
}
但这看起来很无望,显然不起作用。在getImage2.php
中回显图像的最佳方法是什么?
会话数组的示例如下所示:
Array ( [start] => 1435057843
[start_date] => 23/06/15
[start_time] => 12:10:43
[b312a3f205176aa006c8712b3aedb2a4] => images/1370322222.jpg
[5311d8a77e3889723a61b3768faaa4df] => images/1357323650.jpg
[fa14a6a315bf7ddbeb7390af23467f5e] => images/1415737586.jpg
[dd624079e982d78e538f873b7719f179] => images/1369865823.jpg
[5c4011114f6abbb9ecaf2ffbe4d3936f] => images/1369885151.jpg
[d26e3a017ce4851a19511fc0dfedc595] => images/1370317410.jpg
.............
答案 0 :(得分:0)
您不应在此处使用while循环或readfile。相反,试试这个:
session_start();
header("Content-Type: image/jpeg");
echo file_get_contents($_SESSION['$uniqueID']);
我假设您正在尝试一次输出一张图像。
答案 1 :(得分:0)
您不想尝试同时回显所有图像。每个<img>
标记都会向服务器发送一个单独的请求作为页面加载的一部分提供给特定图像,因此只需在服务器上获取PHP代码即可返回请求的特定图像: -
getImage2.php脚本
session_start();
$filename = 'images/missing_image.jpg'; // set a default
if ( isset($_GET['h']) && isset($_SESSION[ $_GET['h'] ]) ) {
$filename = $_SESSION[ $_GET['h'] ]
}
header("Content-Type: image/jpeg");
readfile( $filename );
请注意您的评论
正如您使用此
<img src="getImage2.php?h=<?php echo $uniqueID; ?>" />
将以类似这样的方式写入浏览器
<img src="getImage2.php?h=b312a3f205176aa006c8712b3aedb2a4" />
当getImage2.php收到它时,$ _GET数组将如下所示。
$_GET [
h => b312a3f205176aa006c8712b3aedb2a4
]
所以$_GET['h']
将是你所调用的唯一ID,并使用它来解决正确的文件路径,将它用作$ _SESSION数组的键
答案 2 :(得分:0)
这应该让你开始沿着正确的道路前进:
session_start();
// get passed 'h' parameter and look it up in the session
$handle = isset($_GET['h']) ? $_GET['h'] : null;
$image = isset($_SESSION[$handle]) ? $_SESSION[$handle] : null;
if (file_exists($image) {
// the file can be found
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($image));
session_abort(); // close session without changes
readfile($image);
} else {
// file was not found
if ($image !== null) {
// we should invalidate the session
unset($_SESSION[$handle]);
}
header('HTTP/1.0 404 Not found');
}
但是,通常更好的方法是使用X-Sendfile或X-accel头;您可以从我的older answer了解更多相关信息。