授予777在php中动态创建文件的权限
您好,我有一个脚本,它在本地目录上动态地创建了一个文件 请告诉我,我现在如何授予777对该文件的权限?它在浏览器中不可访问授予777在php中动态创建文件的权限,php,apache,save,Php,Apache,Save,您好,我有一个脚本,它在本地目录上动态地创建了一个文件 请告诉我,我现在如何授予777对该文件的权限?它在浏览器中不可访问 <?php //Get the file $content = 'http://xxx.xxx.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg '; $content1 = explode('/', $content);
<?php
//Get the file
$content = 'http://xxx.xxx.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg ';
$content1 = explode('/', $content);
$end = end($content1);
echo $end;
//print_r($content1[12]);
//Store in the filesystem.
$my_file = $end;
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file
$path = '/var/www/'.$end;
copy($content, $path);
?>
用于设置文件权限。您可以使用
e、 g.chmod($myu文件,0777)代码>使用函数chmod
使用setfacl命令
ex: setfacl -d -m o::rwx your/directory/path
然后,您在该目录中创建的任何东西都需要rwx
权限 我们可以甚至忽略*$handle*它仍然创建文件并将内容复制到$path
<?php
//Get the file
$content = 'http://dev.aviesta.com.mx/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/r/bridal-shoes1.jpg ';
$content1 = explode('/', $content);
$end = end($content1);
echo $end;
//print_r($content1[12]);
//Store in the filesystem.
$my_file = $end;
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file
$path = '/var/www/'.$end;
copy($content, $path);
?>
使用:
授予动态创建文件的权限
$upPath = yourpath;
if(!file_exists($upPath))
{
$oldmask = umask(0);
mkdir($upPath, 0777, true);
umask($oldmask);
}
我只是简单地放下这个:这里。只需在您的文件上使用它,big-fat-bold警告:0777权限为该计算机上具有文件路径的任何人提供打开、修改或执行内容的权限。0777是一个巨大的安全风险,您应该尽可能避免需要它。您不需要0777权限就可以让浏览器读取文件。0644应该可以很好地完成这项工作。@Charles让我们假设我是一名开发人员,需要将调试日志写入一个文件。我认为是777。即使在这种情况下仍然存在风险吗?如果我们在目录中没有写权限,那么chmod
是否仍然有效?
private function writeFileContent($file, $content){
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777); //changed to add the zero
return true;
}
$upPath = yourpath;
if(!file_exists($upPath))
{
$oldmask = umask(0);
mkdir($upPath, 0777, true);
umask($oldmask);
}