2016-03-16 26 views
0

我有一个网站在PHP和CodeIgniter用户上传他们的个人资料图片和个人资料图片存储在名称为pic_uid.jpg的文件夹。.htaccess文件停止访问与直接URL的图片

然后我的脚本从相同的文件夹加载图片。

我想停止直接访问使用.htaccess文件的图片。

一样,如果PIC路径是

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

如果某些一种类型的这种直接的路径,他将不能访问PIC但是当我的脚本调用的这张照片,他可以访问并显示照片。

我已经尝试了很多选择,但是当我停止访问目录时,我的脚本也无法加载图片。

如何实现这一目标?

感谢

回答

2

你可以做这样的事情。有一个目录说,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. 

演示

注意:我在一个免费帐户,所以服务器只运行一段时间。请利用它。

+0

感谢您的答复,但它不工作,首先,当我在我看来,文件中使用此代码,它只能说明变化微小的图像和所有页面的数据已经一去不复返了。也没有载入实际图片 – user3412718

+0

@ user3412718你真的把代码放在什么地方?你可以显示**你的**代码吗? –

+0

我用你写完全相同的代码,但在我看来代码点火器的文件中,有两个问题之一,当我把'标题(“内容类型:image/PNG”);'在视图文件中所有的输出了,第二个文件的内容不正确读取,只有空白图像来了...我检查文件的路径'file_get_contents'和正确的路径 – user3412718

0

对于直接URL
的codeiginiter 副本使用URI这个代码在你的routes.php文件事先知情同意的停止访问

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

此代码封锁了所有的URL访问一个文件夹上传/用户

+0

我以前不工作和如何我会添加所有每张图片的url行数? – user3412718

+0

@ user3412718遵循笨 –

+0

对不起兄弟的本指南,但我没有找URI路径,我期待的回采到图像 – user3412718