为什么 file_put_contents() 会成功,但 touch() 会失败?

发布于 2024-08-09 22:42:14 字数 363 浏览 3 评论 0 原文

我正在运行一个脚本,该脚本对文件的内容进行一些更改,然后将其修改时间重置为之前的时间。间歇性地,我会在日志中发现以下错误:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哥,最终变帅啦 2024-08-16 22:42:14

这是 PHP 的 touch 命令的错误。即使您拥有该文件的写权限,如果 PHP 不是“所有者”,该操作也会失败。

如果您使用 Apache 和 Linux,请在服务器控制台上使用此命令使 PHP 成为文件的所有者:

sudo chown www-data:www-data /YourPATH/YourFILE

更好的是,更新包含您希望 PHP 控制的文件的整个文件夹:

sudo chown -R www-data:www-data /YourPATH/YourFOLDER

旁注:因为 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:

sudo chown www-data:www-data /YourPATH/YourFILE

Better still, update the entire folder containing files you want PHP to control:

sudo chown -R www-data:www-data /YourPATH/YourFOLDER

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.

晨光如昨 2024-08-16 22:42:14

该文件可能是使用错误的权限创建的。尝试对 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.

旧情别恋 2024-08-16 22:42:14

正如 rossoft 所说,PHP 可能不是该文件的所有者。但将权限设置为 777 可能不是最好的解决方案。我更愿意:

function touch_file($file) {
    fclose(fopen($file, 'a'));
}

touch_file('/path/to/file');

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:

function touch_file($file) {
    fclose(fopen($file, 'a'));
}

touch_file('/path/to/file');
沐歌 2024-08-16 22:42:14

就在最近,我遇到了类似的问题,我想我知道答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文