Php .htaccess文件使用直接URL停止对pic的访问

Php .htaccess文件使用直接URL停止对pic的访问,php,.htaccess,codeigniter,Php,.htaccess,Codeigniter,我有一个php和CodeIgniter网站,用户上传他们的个人资料图片,个人资料图片存储在名为pic_uid.jpg的文件夹中 然后我的脚本从同一个文件夹加载图片 我想停止使用.htaccess文件直接访问pic 就像pic路径是 http://localhost/myweb/uploads/users/pic_19.jpg 如果有人键入这个直接路径,他将无法访问pic,但当我的脚本调用此pic时,他可以访问并显示pic 我尝试了很多方法,但当我停止访问目录时,我的脚本也无法加载pic 如何做

我有一个php和CodeIgniter网站,用户上传他们的个人资料图片,个人资料图片存储在名为pic_uid.jpg的文件夹中

然后我的脚本从同一个文件夹加载图片

我想停止使用
.htaccess
文件直接访问pic

就像pic路径是

http://localhost/myweb/uploads/users/pic_19.jpg
如果有人键入这个直接路径,他将无法访问pic,但当我的脚本调用此pic时,他可以访问并显示pic

我尝试了很多方法,但当我停止访问目录时,我的脚本也无法加载pic

如何做到这一点


谢谢

你可以这样做。有一个目录,比如说,
安全的
。然后在该目录中,放置此
.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!");
  }
每个文件都有如下内容:

unsecure.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();
?>
Deny From All
Hello, World.
I am not accessible through normal requests.
My location is in /php/secured/hello.txt.
安全/hello.txt

<?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();
?>
Deny From All
Hello, World.
I am not accessible through normal requests.
My location is in /php/secured/hello.txt.
演示

  • 无法访问的安全文件:
  • 可通过PHP访问的安全文件:
注意:我有一个免费帐户,因此服务器只运行一段时间。请利用它


你可以这样做。有一个目录,比如说,
安全的
。然后在该目录中,放置此
.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!");
  }
每个文件都有如下内容:

unsecure.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();
?>
Deny From All
Hello, World.
I am not accessible through normal requests.
My location is in /php/secured/hello.txt.
安全/hello.txt

<?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();
?>
Deny From All
Hello, World.
I am not accessible through normal requests.
My location is in /php/secured/hello.txt.
演示

  • 无法访问的安全文件:
  • 可通过PHP访问的安全文件:
注意:我有一个免费帐户,因此服务器只运行一段时间。请利用它


用于使用直接URL停止访问pic
使用CodeIgniter的URI 在routes.php中复制此代码

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

此代码阻止了所有访问文件夹的url上载/用户

以停止使用直接url访问pic
使用CodeIgniter的URI 在routes.php中复制此代码

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

此代码阻止了所有访问文件夹的url上传/用户

不起作用&我将如何为每个图片添加所有url行?抱歉,兄弟,但我不寻找URI路由,我正在寻找停止直接访问images@user3412718将图像重定向到另一页的URI路由例如404页不起作用&我将如何为每个图片添加所有url行?抱歉,兄弟,但我不寻找URI路由,我正在寻找停止直接访问images@user3412718URI路由用于将对图像的访问重定向到另一个页面404例如,感谢您的回复,但它不起作用,首先,当我在视图文件中使用此代码时,它只显示一个微小的图像,并且所有页面数据都消失了。而且它没有加载实际图像too@user3412718你实际上把什么作为代码?您能展示一下您的代码吗?我使用的代码与您编写的代码完全相同,但在我的CodeIgniter视图文件中,有两个问题,一个是当我放置
标题(“内容类型:image/png”)视图文件中的所有输出都消失了,第二个文件内容读取不正确,只有空白图像出现。。。我检查
file\u get\u contents
中的文件路径及其正确性path@user3412718
$right\u user=true或false-应更改此部分。我说的是你,是的,我甚至没用那部分。我只是想看看它是否有效,然后我会把如果命令。。。没有这一部分就不行了…谢谢你的回复,但它不起作用,首先,当我在我的视图文件中使用这段代码时,它只显示微小的图像,所有页面数据都消失了。而且它没有加载实际图像too@user3412718你实际上把什么作为代码?您能展示一下您的代码吗?我使用的代码与您编写的代码完全相同,但在我的CodeIgniter视图文件中,有两个问题,一个是当我放置
标题(“内容类型:image/png”)视图文件中的所有输出都消失了,第二个文件内容读取不正确,只有空白图像出现。。。我检查
file\u get\u contents
中的文件路径及其正确性path@user3412718
$right\u user=true或false-应更改此部分。我说的是你,是的,我甚至没用那部分。我只是想看看它是否有效,然后我会把如果命令。。。没有那部分,这是行不通的。。。