我正在运行一个脚本,该脚本对文件的内容进行一些更改,然后将其修改时间重置为之前的时间。间歇性地,我会在日志中发现以下错误:
touch() [function.touch]: Utime
失败:不允许操作
在 file_put_contents()
调用之后立即出现的这条线似乎已经更改了我尝试 touch()
的文件的内容。没有与 file_put_contents()
行相关的错误。
有人遇到过这种情况吗?谁能弄清楚什么权限集允许我写入文件但不能更改其修改时间?我在 Linux 上做这个。
I'm running a script that makes some changes to the contents of a file then resets its modification time to what it was before. Intermittently, I'll find the following errors in my log:
touch() [function.touch]: Utime
failed: Operation not permitted
This on the line immediately after a file_put_contents()
call appears to have changed the contents of the file I tried to touch()
. There are no errors associated with the file_put_contents()
line.
Has anyone had this happen? Can anyone figure out what set of permissions would allow me to write a file but not change its modification time? I'm doing this on Linux.
发布评论
评论(4)
这是 PHP 的
touch
命令的错误。即使您拥有该文件的写权限,如果 PHP 不是“所有者”,该操作也会失败。如果您使用 Apache 和 Linux,请在服务器控制台上使用此命令使 PHP 成为文件的所有者:
更好的是,更新包含您希望 PHP 控制的文件的整个文件夹:
旁注:因为 PHP可以写入文件,这意味着它必须具有用户或组写入权限。既然如此,触摸就不应该有这种行为。这似乎是一个错误。
This is a bug with PHP's
touch
command. Even if you have write permission to the file, it fails if PHP isn't also the "owner".If you're using Apache and Linux, use this command on your server's console to make PHP the file's owner:
Better still, update the entire folder containing files you want PHP to control:
Side Note: Because PHP can write to the file, that means it must have user or group write permission. Since that's the case, touch should not behave this way. It seems like a bug.
该文件可能是使用错误的权限创建的。尝试对 file_put_contents 之后的文件进行 chmod 777,然后触摸该文件。
It could be possible that the file gets created with wrong permissions. Try to chmod 777 the file just after the file_put_contents and then touch the file.
正如 rossoft 所说,PHP 可能不是该文件的所有者。但将权限设置为 777 可能不是最好的解决方案。我更愿意:
As rossoft says, PHP is probably not the owner of the file. But setting the permissions to 777 might not be the best solution. I'd preferr:
就在最近,我遇到了类似的问题,我想我知道答案。
touch()的实际目的是更新文件的修改和访问时间。创建文件只是一个副作用。
如果您使用 Linux,但像使用双引导配置那样写入 NTFS 分区,则根据分区的安装方式,touch() 在更改文件访问时间时可能会出现问题。文件将被创建,但 touch() 仍然会失败,因为底层系统返回错误状态。从命令行中可以观察到同样的情况,您将收到“权限被拒绝”消息。
mount、ntfs-3g 或 touch(Linux 命令)的手册页中似乎没有任何与此相关的文档,但在 touch() PHP 函数页面。
调整挂载选项可能会提供解决方案,但最好使用 is_writable( ) 检查权限,fopen() 创建文件。
Only recently, I've had a similar problem and I think I know the answer.
The actual purpose of touch() is to update the modification and access times of a file. Creating the file is just a side-effect.
If you're using Linux, but writing to an NTFS partition as you might with a dual-boot configuration, depending on how the partition is mounted, touch() might have problems changing the access time on files. The file will be created, but touch() will still fail because the underlying system returns an error status. The same thing can be observed from the command line where you'll get a "permission denied" message.
There doesn't seem to be any documentation regarding this in the man pages for mount, ntfs-3g, or touch (Linux command), but the problem is mentioned in the comments on the touch() PHP function page.
Tweaking mount options might provide a solution, but you're better off using is_writable() to check permissions and fopen() to create files.