要在图像标签中使用这些,您需要将相关的临时文件存储在 web 根目录中的某个位置,因为它们通常位于文件系统中的其他位置,位于 web 根目录之外,并且您不想输出用户的该路径,因为它既无用(img 标签无法映射到该路径),又不安全(您正在泄露有关文件系统的信息)。
您可以通过 a) 将每个文件复制到 webroot 中的某个位置(成本高昂),或 b) 更改您的配置以将所有上传的文件放入web 根目录中的 temp 文件夹(很大的安全隐患)。
在计划此过程时请牢记安全...
请注意其他临时文件的安全性。确保只有与此应用程序的这一部分相关的内容存储在 Web 根目录中。您不希望发现您已经提供了足够的信息来允许某人计算对其他文件的访问权限并且存在某种跨用户数据泄露,因为其他上传的数据在您的网络中可用(即使是很短的时间)根。
这可能是更好的方法:
也许更好的解决方案是获取临时名称,以某种方式对其进行混淆,将其添加到将其传递给网守文件的路径中,然后将其发送。
有点像
/images/path/to/gatekeeper.png?name=[obfuscated file name here]
然后您可以使用 /images/path/to/.htaccess 来确保网守被视为 php 文件而不是 png 文件,方法是添加:
<Files gatekeeper.png>
ForceType application/x-httpd-php
</Files>
现在,网守可以对文件名进行去混淆处理,从临时路径中提取并发送它,而无需向用户透露任何内容、移动实际文件或更改您的配置。
请确保您不希望文件保留,因为如果它们稍后刷新,并且文件已移动(几乎肯定会移动),它将只是一个损坏的图像。也许与看门人一起检查存在,如果找不到,则给他们一个“文件不再可用”的图像。您还可以强制仅发送图像文件,或在网守级别强制发送其他安全问题。
您可以这样做(从内存中键入,可能不会运行,并且没有处理错误,缺失值等,但您应该能够以此为起点......):
<?php
// This is all inside gatekeeper.png, which is just a .php file with a .png extension
// It's not very good code, just an example to point you in the right direction.
// Specify this will be a png file for the browser...
header("content-type: image/png");
//Read the file name from the $_GET and un-obfuscate it.
$file = "/path/to/temp/directory/outside/file/root/that/we/hopefully/have/access/to/";
$file .= $_GET["name"];
// Check that the file exists, and send a "not found" image if not.
// http://us.php.net/manual/en/function.file-exists.php
if (!file_exists($file)) {etc...}
// Read the image file, send it to the user, and close it...
// http://us.php.net/manual/en/function.imagepng.php
$im = imagecreatefrompng($file);
imagepng($im);
imagedestroy($im);
die();
?>