简体   繁体   English

使用 PHP 在 Windows 中设置文件权限

[英]Setting File Permissions in Windows with PHP

I have a script that uploads a *.csv to import into a DB table that I have which works great in linux through chmod($target, 0777);我有一个脚本可以上传 *.csv 以导入到我拥有的数据库表中,该表通过chmod($target, 0777); but I can't for the life of me find the solution to do exactly this but on a Windows based Apache server.但我一辈子都找不到解决方案来做到这一点,而是在基于 Windows 的 Apache 服务器上。

Some other posts have people responding "don't put in the 0777 and it should work" but that's not the case for me.其他一些帖子有人回答“不要放入 0777,它应该可以工作”,但对我来说并非如此。 Thanks!谢谢!

Thanks to the comment left on my original post I was able to figure it out with a little more help from https://web.archive.org/web/20171121192635/http://www.howyoudo.info/index.php/how-to-fix-windows-server-upload-file-inherit-permissions-error/感谢在我原来的帖子上留下的评论,我能够在https://web.archive.org/web/20171121192635/http://www.howyoudo.info/index.php/的更多帮助下弄清楚它如何修复-windows-server-upload-file-inherit-permissions-error/

The problem only happens when you use PHP to upload a file.仅当您使用 PHP 上传文件时才会出现此问题。 When you upload a file, PHP sends the file to a temporary directory on the hard drive (for me it is C:\Windows\Temp) and then copies it over to it's intended directory.当您上传文件时,PHP 会将文件发送到硬盘驱动器上的临时目录(对我来说是 C:\Windows\Temp),然后将其复制到预期的目录中。 Once the file has landed in the temporary directory, it is assigned the permissions of that directory.一旦文件进入临时目录,它就会被分配该目录的权限。 The problem is when Windows copies that file, it keeps the temporary directory's permissions and doesn't inherit your web directory's permissions.问题是当 Windows 复制该文件时,它会保留临时目录的权限,并且不会继承您的 Web 目录的权限。

The easiest way to fix this problem is to add to the temporary directory your intended web directory's permissions.解决此问题的最简单方法是将您预期的 Web 目录的权限添加到临时目录。 There's no need to erase the permissions already in the temporary directory, just add the web directory's permissions to them.无需删除临时目录中已有的权限,只需将 web 目录的权限添加到它们即可。 In other words, follow these steps换句话说,请按照以下步骤操作

  1. To change the permissions of your temporary upload directory, find the “upload_tmp_dir” in your php.ini file.要更改临时上传目录的权限,请在 php.ini 文件中找到“upload_tmp_dir”。
  2. Set it to the directory of your choosing (outside your web folders of course) or leave it at default (for me it is C:\Windows\Temp).将其设置为您选择的目录(当然在您的 Web 文件夹之外)或将其保留为默认目录(对我来说是 C:\Windows\Temp)。
  3. Browse to this folder and add the permissions of your web folders to it.浏览到此文件夹并将您的 Web 文件夹的权限添加到其中。

While Brian Leishman's answer will work, if you do not have the ability to edit permissions on the temp folder, you can make your uploaded file inherit permissions from its new location manually with the following on the command line:虽然 Brian Leishman 的回答会起作用,但如果您无法编辑临时文件夹的权限,您可以使用命令行中的以下命令手动使上传的文件从其新位置继承权限:

icacls "target.txt" /q /c /reset

So, using PHP's exec() function:因此,使用 PHP 的exec()函数:

exec( 'icacls "target.txt" /q /c /reset' );

For details of the various icacls flags, see: https://technet.microsoft.com/en-us/library/cc753525.aspx有关各种icacls标志的详细信息,请参阅: https ://technet.microsoft.com/en-us/library/cc753525.aspx

Tip: Using the /t flag, you can use pattern matching to process multiple files.提示:使用/t标志,您可以使用模式匹配来处理多个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM