How to give file permissions in php

Hi i have a script which dynamically created 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

<?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);

 ?>

asked Jan 7, 2013 at 9:30

How to give file permissions in php

Rohit GoelRohit Goel

3,3147 gold badges51 silver badges104 bronze badges

3

Use the function chmod
chmod

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

answered Jan 7, 2013 at 9:32

1

Use chmod() to set file permissions.

answered Jan 7, 2013 at 9:31

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;
}

answered Dec 5, 2014 at 10:32

How to give file permissions in php

Give permission in to dynamically created file

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

answered Nov 19, 2015 at 9:40

You can use chmod() to do this.

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

answered Jan 7, 2013 at 9:32

How to give file permissions in php

Thomas ClaysonThomas Clayson

29.3k26 gold badges144 silver badges223 bronze badges

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

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

then any thing u create in that directory it takes rwx permission.

How to give file permissions in php

Dhara

4,0932 gold badges34 silver badges69 bronze badges

answered Jan 7, 2013 at 11:06

we can even ignore *$handle* it will still create the file and copy the content to the $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);

     ?>

answered Jan 9, 2013 at 10:10

How to give file permissions in php

Rohit GoelRohit Goel

3,3147 gold badges51 silver badges104 bronze badges

How do I give PHP permission to 777?

If the operation is for already created file then:.
PHP Syntax. chmod ( string $filename , int $mode );.
Ex: chmod('text.txt', 0777);.
If you are creating a new file dynamically then:.
PHP Syntax. ... .
Ex: mkdir($_SERVER['DOCUMENT_ROOT']. ... .
Hope its clear..

How do I give permission to a folder in PHP?

You can use chmod() function.

What permissions should php files have?

Set php files to 640. For maximum security you should set minimum permissions, which is 640. The owner 6 would be the one uploading the files.

What is the use of chmod 777?

The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions.