这里是一种更加全局和高效的解决方案。我需要在保存递归目录下的文件时控制文件和目录的权限。
League SftpAdapter会递归创建目录,如果不存在的话。但主要问题是它不会为目录添加
permPublic => 0755
,只会为文件添加,因此如果文件位于新创建的目录中,则
www-data
用户最终无法访问该文件。解决方法是深入代码查看发生了什么:
'disks' => [
'remote-sftp' => [
'driver' => 'sftp',
'host' => '222.222.222.222',
'port' => 22,
'username' => 'user',
'password' => 'password',
'visibility' => 'public', // set to public to use permPublic, or private to use permPrivate
'permPublic' => 0755, // whatever you want the public permission is, avoid 0777
'root' => '/path/to/web/directory',
'timeout' => 30,
'directoryPerm' => 0755, // whatever you want
],
],
在
League\Flysystem\Sftp\StfpAdapter
中,有两个重要的属性需要清楚地看到:
protected $configurable = ['host', 'hostFingerprint', 'port', 'username', 'password', 'useAgent', 'agent', 'timeout', 'root', 'privateKey', 'passphrase', 'permPrivate', 'permPublic', 'directoryPerm', 'NetSftpConnection'];
protected $directoryPerm = 0744;
$configurable
是配置SFTP文件系统驱动的所有可能键。您可以在配置文件中将 directoryPerm
从 0744
更改为 0755
。
'directoryPerm' => 0755,
不过,由于在StfpAdapter中存在一种类似于Bug的问题https://github.com/thephpleague/flysystem-sftp/issues/81,它不会使用createDir
上的$config参数:
$filesystem = Storage::disk('remote-sftp');
$filesystem->getDriver()->getAdapter()->setDirectoryPerm(0755);
$filesystem->put('dir1/dir2/'.$filename, $contents);
或者故意将其设置为公共属性:
$filesystem->put('dir1/dir2/'.$filename, $contents, 'public');