Adeline Duterre
Adeline Duterre

Reputation: 31

permission denied : copy / writing file (laravel / php)

I use on my application some fonctions about files. creation of files, copy/create from a directory to another, update. On my localhost, all is okay, that's work.

The main purpose is to have a manager for translation on the website.

I have error : 1) if files doesn't exist the error is copy(/blabla/path.php): failed to open stream: Permission denied

2) if I create files before (touch + chown on usr:usr) with basic rights (644) or with updated rights (775) : file_put_contents(/blabla/path.php) : failed to open stream: Permission denied

On my localhost :

I also tried to clean php cache (php artisan cache:clear)

edit :

I use 2 functions : copy (who copy default translate file in the directory of second langage) and file_put_contents (who edit file)

public function replaceSlugs(Array $slugs,$filename, $lang)
    {
        $file=$this->path2File($filename,$lang);
        if(!file_exists($file)){$this->duplicateFile($lang,$filename);}
        $translate="<?php return ".var_export($slugs,true).";";
        return file_put_contents ( $file , $translate, LOCK_EX );
    }


    private function duplicateFile($lang,$filename)
    {
        copy($this->path2File($filename),$this->path2File($filename,$lang));
    }

    private function path2File($filename,$lang="en")
    {
        return resource_path('lang/'.$lang.'/'.$filename.'.php');
    }

Upvotes: 3

Views: 966

Answers (1)

Vicky Gill
Vicky Gill

Reputation: 734

Solution is Risky but it will do the trick

Give your folders full permission & clear Cache

Something like this.

sudo chmod -R 777 public/lang/en

if you don't give 777 permission you will also stuck while unit Test.

Upvotes: -1

Related Questions