PHP将文件上传到Web服务器根目录之外的目录 [英] PHP upload a file to a directory outside the webserver root

查看:736
本文介绍了PHP将文件上传到Web服务器根目录之外的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对你们有一个问题...我正在尝试使用PHP编写一个网站,该网站将允许人们将图像上传到我的服务器.我的上传部分正常工作,所以这不是问题.问题是当我尝试将上传的文件写入apache文档根目录之外的目录时.

Apache的文档根目录为/var/www/,我尝试将这些图像保存到的目录为/mnt/storage/images/.我已经创建了目录,将组更改为www-data,并将目录的权限更改为777(即使我知道这是可怕的安全措施).我尝试使用apache配置文件/etc/apache2/apache2.conf进行了一些操作,即添加标签和Alias行,但似乎没有任何效果.每次尝试将文件上传到ima​​ges目录时,都会出现权限错误.我什至尝试过手动将一些文件放在那里,并使用别名在Web浏览器中导航到目录,但出现403错误.

我知道我做错了什么,或者我没有做任何事情来完成这项工作.几天来,我一直在Google上寻求帮助,而我所发现的只是无用的信息,或者一点点的信息都根本无济于事.

顺便说一句...我在使用Apache 2.2.17的Ubuntu 11.04上,并且具有root访问权限和所有爵士乐.

谢谢, 罗比

这是我放入的apache配置的一部分:

Alias /images "/mnt/storage/images/"
<Directory "/mnt/storage/images/">
    Allow from all
</Directory>

解决方案

您必须为所有涉及的目录授予Apache访问权限:

/mnt
/mnt/storage
/mnt/storage/images

如果Apache对storagemnt目录没有权限,则在images上执行777毫无意义.

当然,只有images才需要具有写权限,因为这实际上是您进行文件操作的地方. mntstorage最多需要读取权限,并且可以通过将Apache的用户帐户放入一个组并将chsrps图片目录切换到该新组来摆脱全局(xx7)权限.

So I have a question for you guys... I am trying to write a web site in PHP that will allow people to upload images to my server. I have the upload part working, so that's not the issue. The issue is when I try and write the uploaded files to a directory outside of apache's document root.

Apache's document root is /var/www/, and the directory I am trying to save these images to is /mnt/storage/images/. I have created the directory, changed the group to www-data, and changed the permissions on the directory to 777 (even though I know that's horrible security wise). I tried a few things with the apache configuration file, /etc/apache2/apache2.conf, namely adding a tag and an Alias line, but nothing seems to work. Every time I try and upload a file to the images directory, it gives me a permission error. I've even tried putting a few files there manually and navigating to the directory, using the alias, with the web browser, and I get a 403 error.

I know I am either doing something wrong, or I haven't done something to make this work. I have googled around for help on this for a few days now, and all I find is useless information, or bits and pieces of information that barely help at all.

Btw... I'm on Ubuntu 11.04 using Apache 2.2.17, and I have root access and all that jazz.

Thanks, Robbie

EDIT: Here is the part of the apache config that I put in:

Alias /images "/mnt/storage/images/"
<Directory "/mnt/storage/images/">
    Allow from all
</Directory>

解决方案

You have to grant Apache access permissions on ALL of the directories involved:

/mnt
/mnt/storage
/mnt/storage/images

There's no point in doing 777 on images if Apache has no rights on the storage or mnt directories.

Of course, only images would need to have write permissions, as that's where you're actually doing file operations. mnt and storage would need at most read permissions, and you can get away from doing global (xx7) permissions by putting Apache's user account into a group and chgrp the images directory to that new group.

这篇关于PHP将文件上传到Web服务器根目录之外的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆