我不久前问过这个问题并得到了我认为可行的答案,但我仍然遇到问题。也许这是我做错的事,但我仍然没有这个权利。
我想限制对整个目录的访问。该目录中包含图像和 pdf 文件。我需要创建 pdf 文档的链接并将图像嵌入到锚标记中。有人告诉我为此使用标头。使用 header 会立即输出非常适合 pdf 文档的内容,但我必须从同一目录嵌入 3 个图像,但这不起作用,因为 header 在 html 之前输出图像,这是正确的行为。
我需要一种将图像分配给变量的方法,以便在输出 html 后将其嵌入到需要的位置。有任何想法吗?
这是我最后一个问题的链接。如何限制查看目录中的文件 https://stackoverflow.com/questions/5485045/how-to-restrict-viewing-files-within-a-directory
将图像移至主机的公共文档根之外,或使用 .htaccess 限制对它们的访问,例如
<FilesMatch "\.(gif|png|jpe?g)$">
Order Allow,Deny
Deny from all
</FilesMatch>
并使用 PHP 脚本发送图像,该脚本将检查用户会话并仅在用户登录时发送图像。
//... your session checking routine just like in other scripts
if (!$logged) {
//show error
exit();
}
//Simple extention-to-mimetype map:
$mimetypes = array(
'.jpg' => 'image/jpeg'
'.jpeg'=> 'image/jpeg'
'.pdf' => 'application/pdf'
//add other extensions if needed
);
$file = basename($_GET['file']); //preventing tricks with ../../anypath/anyfile
$ext = substr($file, strrpos($file, '.'));
if (file_exists($images_dir . $file) && isset($mimetypes[$ext]) ) {
header('Content-Type: ' . $mimetypes[$ext]);
echo file_get_contents($images_dir . $file);
} else {
//show error
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)