PHP/CHMOD 问题
我正在开发一个基于 PHP 的网站。在管理中,有一个部分检查表单字段并根据该字段查找服务器上的文件夹。该文件夹将位于一个子目录中。如果它不存在,则需要创建它。之后,无论是否存在,PHP 都会将文件写入该文件夹。
这些文件夹将保存将在主站点上查看和/或下载的图像和 PDF 文件。
以下是目录结构示例:merchants/east/user123 在上面的商家和east肯定会存在,并且user123可能存在或以其他方式被创建。
鉴于这些信息,我的问题是关于文件夹权限的。
- 为了获得最佳安全性,文件夹应该设置为什么。
- 我是否应该在操作过程中将它们打开得更宽,然后在完成更安全的操作后对其进行 chmod(在 PHP 中)?
- 上层文件夹应该设置成什么?
I am working on a PHP based website. In the admin there is a section that checks a form field and based on the field looks for a folder on the server. This folder will be in a sub-directory. If it does not exist it needs to be created. After that, previously existing or not, PHP will write file to the folder.
These folders will hold images and PDF files that will be viewed and/or downloaded on the main site.
Here is an example directory structure: merchants/east/user123
In the above merchants and east would definitely exist and user123 may exist or otherwise be created.
Given that info my questions are about folder permissions.
- What should folders be set to for the best security.
- Should I open them up wider during operations then chmod them (in PHP) after I'm done to something more secure?
- What should upper level folders be set to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
770
对于这些文件来说是一个安全的选择。将其设置为禁止任何公共访问。我会用 PHP 实现某种文档传递系统。 PHP 将能够访问非公开文件,然后将它们发送给用户。上层文件夹可以设置为相同。
更新
正如其他人所说,您可以轻松地将它们修改为
600
而不会出现任何问题。这是更安全的处理方式(防止系统上的其他用户访问文件)。它还省略了“执行”,无论如何,文件读取都不需要它。除非有明确的理由不这样做,否则我个人的做法是保留额外的内容。770
would be a safe bet for the files. Setting it to that would disallow any public access. I would implement some sort of document delivery system in PHP. PHP will be able to access the non-public files and then send them to the user.The upper level folders could be set to the same.
Update
As others have said, you can easily chmod them to
600
without any issues. That's the more secure way of handling it (prevents other users on the system from accessing the files). It also omits "execute", which isn't needed for file reading anyway. It's my personal practice to leave the extras in unless there's a defined reason not to.上层文件夹需要具有 apache 用户的读取、写入和执行权限。顶级文件夹可以由 apache 拥有,并具有 755 等权限,以允许 Web 服务器读取、写入和列出文件。
如果您特别担心 Web 服务器上的其他本地用户或服务无法查看此目录中的文件,您可能会考虑权限 750 或 700。
对于文件权限:644或600,因为按照惯例它们不需要执行权限。
一个不错的折衷方案可能是对目录使用 750,对所有者设置为 apache 的文件使用 640,并更改组 (chgrp),以便文件的组允许您通常用来编辑网站文件的用户访问。
我想不出 php 脚本增加然后减少权限有什么显着的优势。
我认为你应该考虑 @chunk 的评论,关于将上传的文件完全保留在公共 html 目录中,并通过文件传递脚本将它们返回。否则,您需要对文件内容进行仔细验证,并加强该特定目录的 apache 配置 - 也许使用一些 mimetype 检查来确保文件确实是 docs 和 pdf。
The upper level folder would need to have read, write and execute permissions for the apache user., the top level folder could be owned by apache, and have permissions like 755 to allow the the webserver to read, write and list files.
You might think about permissions 750 or 700 if you are particularly concerned about other local users or services on the web server from seeing the files in this directory.
For file permissions: 644 or 600 as conventionally they do not need execute permission.
A nice compromise might be to use 750 for directories and 640 for files with owner set to apache, and change the group (chgrp) so that the group for the file allows access to the user that you normally edit the website files with.
I can't think of any significant advantage of the php script increasing and then reducing the permissions.
I think you should consider @chunk's comment about keeping the uploaded files own of the public html directory completely, and serving them back via an file delivery script. Otherwise you would need some careful validation of the content of the files and to tightening up the apache configuration for that particular directory - perhaps using some mimetype checking to make sure that the files really are docs and pdfs.