保护图像免受直接URL访问?

时间:2016-03-09 08:25:16

标签: apache .htaccess hotlinking

我目前正在使用php,apache服务器和symfony开展项目。我想要实现的是防止我的图像直接通过网址栏中的http://localhost:8000/img/logoSmall.png访问。我已经尝试过使用override func viewDidLoad() { super.viewDidLoad() self.arrimages = NSArray (arrayLiteral: UIImage(named: "1.png")!,UIImage(named: "2.png")!,UIImage(named: "3.png")!,UIImage(named: "4.png")!) print("Images array = \(self.arrimages)") self.posterImageview.image = UIImage(named: "1.png")! self.animatePoster(self.arrimages as! [UIImage], count: 1) } func animatePoster (imageArray: [UIImage], count: Int = 0){ print("count value = \(count)") UIView.transitionWithView(self.posterImageview, duration: 5, options: UIViewAnimationOptions.TransitionFlipFromBottom, animations: { }, completion: { (finished: Bool) -> () in let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { self.posterImageview.image = imageArray[count] } if (count == (imageArray.count - 1)) // Last image { let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(4 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { self.animatePoster(imageArray) } } else { let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(4 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { self.animatePoster(imageArray, count: count + 1) } } }) } ,但它不起作用,我不知道我做错了什么,我也试过this帖子中的一些东西,如:

Option All -Indexes

并改变

RewriteEngine on RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] RewriteRule \.(gif|jpg|png)$ - [F]

Options Includes Indexes FollowSymLinks MultiViews

我也确定我的httpd.conf文件有Options Includes FollowSymLinks MultiViews

我真的不知道还能做什么。我已经被困在这里太久了,如果你在我的解决方案中看到任何错误,或者你有任何建议,我会非常感激他们。提前谢谢。

修改

我确实想继续在我的网站上显示图片,我不想要的是允许其他用户使用直接网址访问它们。

2 个答案:

答案 0 :(得分:0)

我有类似的问题,但在我的情况下,我使用了一些文件夹(上传)用于用户上传的文件(个人资料图片等),然后我只需要为某些用户(帐户所有者和他的联系人)显示这些文件。

我的解决方案: 1)在security.yml中关闭除超级管理员以外的所有用户的上传文件夹

security:
   access_control:
       - { path: ^/uploads/, role: ROLE_SUPER_ADMIN }

2)如果您需要用户能够从服务器创建控制器下载文件来处理此

use Symfony\Component\HttpFoundation\BinaryFileResponse;

class DownloadController extends Controller
{
    public function downloadAction(Request $request, $filepath = null)
    {
        if ($filepath){
            $response = new BinaryFileResponse($filepath);

            return $response;
        }
    }
}

答案 1 :(得分:-1)

apache2.conf中(至少是我测试过的内容):

<FilesMatch "\.(gif|jpg|png)$">
    Require all denied
</FilesMatch>

注意:这将阻止访问HTML文件中的文件。它们可以在页面上显示的唯一方法是将它们包含在HTML中,并在PHP中生成data URIs

相关问题