20

嗨,我有一个脚本,它在我的本地目录上动态创建了一个文件请告诉我,我现在如何授予该文件 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);
$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);

 ?>
4

7 回答 7

47

使用函数 chmod
chmod

$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777); 
于 2013-01-07T09:32:36.397 回答
3

用于chmod()设置文件权限。

于 2013-01-07T09:31:51.840 回答
2

利用:

private function writeFileContent($file, $content){
   $fp = fopen($file, 'w');
   fwrite($fp, $content);
   fclose($fp);
   chmod($file, 0777);  //changed to add the zero
   return true;
}
于 2014-12-05T10:32:14.433 回答
2

授予动态创建文件的权限

                  $upPath = yourpath;
                    if(!file_exists($upPath)) 
                    {
                        $oldmask = umask(0);
                        mkdir($upPath, 0777, true);
                        umask($oldmask);
                    }  
于 2015-11-19T09:40:50.820 回答
0

我们甚至可以忽略 * $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);

     ?>
于 2013-01-09T10:10:38.267 回答
0

您可以使用chmod()来执行此操作。

例如chmod($my_file, 0777);

于 2013-01-07T09:32:11.277 回答
0

使用设置default permission为您的目录(即rwxsetfacl command

ex: setfacl -d -m o::rwx your/directory/path

然后你在该目录中创建的任何东西都需要rwx许可。

于 2013-01-07T11:06:21.247 回答