繁体   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