给 777 权限以在 php 中动态创建文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14193131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:50:01  来源:igfitidea点击:

Give 777 permission to dynamically created file in php

phpapachesave

提问by Rohit Goel

Hi i have a script which dynamicallycreated a file on my local directory Please tell me how can i give 777 permission to that file right now it is unaccessible in the browser

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

 ?>

回答by Alessandro Minoccheri

Use the function chmod
chmod

使用函数 chmod
chmod

$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
chmod($file, 0777); 

回答by Oliver Charlesworth

Use chmod()to set file permissions.

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

回答by Elton da Costa

use:

用:

private function writeFileContent($file, $content){
   $fp = fopen($file, 'w');
   fwrite($fp, $content);
   fclose($fp);
   chmod($file, 0777);  //changed to add the zero
   return true;
}

回答by Hiren Makwana

Give permission in to dynamically created file

授予动态创建文件的权限

                  $upPath = yourpath;
                    if(!file_exists($upPath)) 
                    {
                        $oldmask = umask(0);
                        mkdir($upPath, 0777, true);
                        umask($oldmask);
                    }  

回答by Thomas Clayson

You can use chmod()to do this.

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

e.g. chmod($my_file, 0777);

例如 chmod($my_file, 0777);

回答by SameerJ

set default permissionto u r directory (i.e rwx) using setfacl command

设置default permission为您的目录(即rwx)使用setfacl command

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

then any thing u create in that directory it takes rwxpermission.

然后您在该目录中创建的任何内容都需要rwx许可。

回答by Rohit Goel

we can evenignore *$handle*it will still createthe fileand copythe content to the $path

我们可以甚至忽略 * $处理*它仍然会创建文件复制内容到 $ 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);

     ?>