PHP 讀取受.htaccess保護的資料夾下的圖片

15Feb11

我想放上一些資料,只想讓已在網站登入的會員讀取。儘管以PHP隱藏圖片的真實路徑都覺得不夠安全,只要別人知道圖片的真實位置就能直接可以開啟了。於是我想用.htaccess保護圖片的目錄,需要password才能讀取,再用PHP在網站檢查會員的身份,再讓這些會員看到檔案。但是已經登入的會員,需要下載那些檔案還是要經過HTTP的驗證,可是別人已經登入了,又要人家輸入多一次密碼嗎?

今天想到用include("./path/圖片.jpg"),再用在別的檔案用<img src="img.php" />就可以讀到了。

在網上再查了關於include 的用法,想確定這種做法沒問題,在include() 的使用手冊有人提到可以

<?php
header('Content-type: image/jpeg');
header('Content-Disposition: inline;');
include '/some_image.jpg';
echo 'This file was provided by example@user.com.';
?>

但是不鼓勵include圖片,這種方法不太安全。有可能讓人修改圖片讓PHP執行script。
建議用echo file_get_contents() 來取代。



No Responses Yet to “PHP 讀取受.htaccess保護的資料夾下的圖片”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.