Php 像sudo一样触摸文件
在使用[Symfony的文件系统][1]时,如何像sudo那样触摸文件 到目前为止,我已经:Php 像sudo一样触摸文件,php,symfony,symfony-filesystem,Php,Symfony,Symfony Filesystem,在使用[Symfony的文件系统][1]时,如何像sudo那样触摸文件 到目前为止,我已经: $fs = new Filesystem(); $confFile = sprintf('/etc/apache2/sites-available/%s.conf', $input->getArgument('name')); $fs->touch($confFile); 但此代码失败,错误为:权限被拒绝 [1] : 您需要更改/etc/apache2/sites available/上的
$fs = new Filesystem();
$confFile = sprintf('/etc/apache2/sites-available/%s.conf', $input->getArgument('name'));
$fs->touch($confFile);
但此代码失败,错误为:权限被拒绝
[1] :
您需要更改
/etc/apache2/sites available/
上的权限,以向apache用户添加写访问权限
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" /etc/apache2/sites-available/
从Symfony文件系统组件:
public function touch($files, $time = null, $atime = null)
{
foreach ($this->toIterator($files) as $file) {
$touch = $time ? @touch($file, $time, $atime) : @touch($file);
if (true !== $touch) {
throw new IOException(sprintf('Failed to touch "%s".', $file), 0, null, $file);
}
}
}
如您所见,touch()
方法只是PHP的buildintouch()
函数的简单包装。如果您需要通过sudo使用提升权限运行touch,您必须直接呼叫它:
shell_exec('sudo touch ' . escapeshellarg($file));