php 的 rename 函数在什么情况下会输出 Operation not permitted 的错误,而不是警告?

发布于 2022-09-07 11:59:16 字数 1141 浏览 13 评论 0

基本环境
Ubuntu 16.04.1
apache 2.4
php 7.1.7

php 文件的根目录是通过 samba 挂载的一个 windows 机器下的目录

//192.168.44.1/dev/144 on /home/wwwroot type cifs (rw,nosuid,nodev,noexec,relatime,vers=default,cache=none,domain=,uid=0,forceuid,gid=0,forcegid,addr=192.168.44.1,file_mode=0777,dir_mode=0777,nounix,mapposix,rsize=1048576,wsize=1048576,echo_interval=60,actimeo=1)

目录本身是可以访问的……拥有所有rw权限,只是没有 x 权限

报错部分……

84:        if(file_exists($targetPath))return true;
85:        if (!$tran) return rename($fromPath, $targetPath);【此行】
[2018-03-26 09:44:45] local.ERROR: ErrorException: rename(/tmp/php2aXCEp,/home/wwwroot/cm/public_static/upload/user/1b/11384324d7be4098700a14ecfb418a.png): Operation not permitted in /home/wwwroot/cm/app/Helpers/FileReceiver.php:85

http://php.net/manual/en/func...

根据文档来讲的话,在版本 4.3.3 之后 rename 到不支持的文件系统上“可能”会产生一个警告……
但是现在的情况是这个函数直接就抛出了一个错误,但是文件却又复制成功了……
官方这文档下面也有几个遇到同样情况的仁兄【就是都被踩垫底了……】,虽然加个 @ 再重新 file_exists 基本就可以解决问题,不过这种情况是如何产生的?

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

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

发布评论

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

评论(1

忆沫 2022-09-14 11:59:16

许久没有管这个事情……自问自答了……
翻了一下评论曰实际上是一个fat文件系统权限的问题……说是说不能叫做bug……但是总之就当个注意点吧……
参考ben at indietorrent dot org 的回答……
也就是复制到fat格式的文件系统上时会产生如此的错误……

From the Changelog notes:

"Warnings may be generated if the destination filesystem doesn't permit chown() or chmod() system calls to be made on files — for example, if the destination filesystem is a FAT filesystem."

More explicitly, rename() may still return (bool) true, despite the warnings that result from the underlying calls to chown() or chmod(). This behavior can be misleading absent a deeper understanding of the underlying mechanics. To rename across filesystems, PHP "fakes it" by calling copy(), unlink(), chown(), and chmod() (not necessarily in that order). See PHP bug #50676 for more information.

On UNIX-like operating systems, filesystems may be mounted with an explicit uid and/or gid (for example, with mount options "uid=someuser,gid=somegroup"). Attempting to call rename() with such a destination filesystem will cause an "Operation not permitted" warning, even though the file is indeed renamed and rename() returns (bool) true.

This is not a bug. Either handle the warning as is appropriate to your use-case, or call copy() and then unlink(), which will avoid the doomed calls to chown() and chmod(), thereby eliminating the warning.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文