![](/img/trans.png)
[英]PHP: How can I block direct URL access to a file, but still allow it to be downloaded by logged in users?
[英]How can I block direct access to images in a directory but allow PHP to display them?
我正在使用帶有以下代碼的.htaccess文件:
Order deny, allow
Deny from all
Allow from localhost
但是,當我顯示圖像時,我只是在標簽中放置了指向圖像的鏈接,但是圖像沒有顯示出來。
如何顯示圖像但不允許直接訪問它? 我有什么辦法可以只復制文件的原始字節並顯示它們嗎?
請確保將Embedded.png與該示例源保存在同一文件夾中。
<?php
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
?>
<html>
<h1>Embedded Image:-</h1>
<img src="<?php echo data_uri('embedded.png','image/png'); ?>" alt="Embedded image" />
</html>
發送適當的圖像的Content-Type
標頭,然后使用readfile()
傳輸要輸出的字節。
另外,如果您打算為此在URL中使用文件路徑,請確保首先清理該文件路徑,以使例如/etc/passwd
無法使用您的腳本讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.