簡體   English   中英

.htaccess文件停止使用直接URL對圖片的訪問

[英].htaccess file stop access of pic with direct URL

我在php和CodeIgniter中有一個網站,用戶在其中上傳他們的個人資料圖片,而個人資料圖片存儲在名為pic_uid.jpg的文件夾中。

然后我的腳本從同一文件夾加載圖片。

我想停止使用.htaccess文件直接訪問圖片。

就像pic path是

http://localhost/myweb/uploads/users/pic_19.jpg

如果有人鍵入此直接路徑,他將無法訪問圖片,但是當我的腳本調用此圖片時,他可以訪問並顯示該圖片。

我嘗試了許多選項,但是當我停止訪問目錄時,我的腳本也無法加載圖片。

如何實現呢?

謝謝

你可以做這樣的事情。 有一個說secured的目錄。 在該目錄中,放置以下.htaccess

Deny From All

現在,將所有圖像文件存儲在此處:

+ secured/
  - image-1.png
  - image-2.png
  - image-3.png

在您的PHP腳本中,使用以下代理:

<?php
  ob_start();
  /* true if the conditions met, like coming from the script or something */
  $right_user = true or false;
  if ($right_user) {
    header("Content-type: image/png");
    echo file_get_contents("secured/" . $_GET["file"]);
    die();
  } else {
    header("Content-type: text/plain");
    die("Ha ha! Can't steal!");
  }

為了重申我所做的一切,我在Cloud9上創建了一個倉庫 在那,我得到了這些文件:

└── php
    ├── index.php
    ├── insecure.php
    └── secured
        ├── .htaccess
        └── hello.txt

每個文件都有這樣的內容:

insecure.php

<?php
    header("Content-type: text/plain");
    if (file_exists("secured/" . $_GET["file"]))
        echo file_get_contents("secured/" . $_GET["file"]);
    else
        echo "404! File Not Found.";
    die();
?>

secured/.htaccess

Deny From All

secured/hello.txt

Hello, World.
I am not accessible through normal requests.
My location is in /php/secured/hello.txt.

演示版

注意: 我使用的是免費帳戶,因此服務器僅運行一段時間。 請利用它。

用於通過直接URL停止對圖片的訪問
使用codeiginiter的URI在您的route.php中復制此代碼

$route['uploads/users/(:any)'] = "page_not_found";

此代碼阻止了所有訪問文件夾上載/用戶的網址

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM