permission denied : copy / writing file (laravel / php) - 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.
rights of parent directories : 755
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 :
right of directories : 755
right of files : 644
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');
}

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.

Related

APACHE Permissions are all set but still don't have permission

I recently faced this issue, as you can see in the picture, I granted all sorts of permissions to all files, but still I get this error.
Here's the code:
$zipdir=dirname(dirname(__FILE__)).'/tmp';
$zipname="$zipdir/$input[user]-".time().'.zip';
list($pdf,$pdfname)=test_pdf($request);
$zip=new ZipArchive();
if ($zip->open($zipname,ZipArchive::CREATE)!==true) return $error='Could not open zip archive for writing';
$zip->addFromString("pdf/$pdfname", $pdf->Output('','S'));
$zip->addFile("test-docs/",$testformname);
$zip->close();
if (!file_exists($zipname)) return $error='Could not create zip archive';
https://i.stack.imgur.com/kmxUg.png
And here's the log:
PHP Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /var/www/html/test/app/test.php
Problem just solved!
I needed to grant 775 or above permission to the whole "html" folder in this path :
"/var/www/html/..."
Not just files and folders in it because i tried that completely.
Also apache:apache permission is not needed, root does the work.
This comment helped a lot:
move_uploaded_file gives "failed to open stream: Permission denied" error

Writing Log File Error file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to open stream: Permission denied

I have inherited a PHP application and need to investigate various issues. I am not a PHP programmer so bear with me.
I wrote a function to log messages to a custom log file.
<?php
function my_logger($log_msg)
{
error_log("USER INFO:::::",0);
error_log(get_current_user());
error_log(exec('whoami'));
file_put_contents('/var/www/html/myproject/ulogs/my-log.log', date('G:i:s') . ">>$ " . $log_msg . "\n", FILE_APPEND);
}
After a lot of 500 errors and no information I discovered that the following line is being written to the error_log.log
PHP Warning:
file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to
open stream: Permission denied in
/var/www/html/myproject/app/lib/log_fns.php on line 11
I have put in a call to get_current_user and whoami which shows me:
[07-Apr-2021 14:26:04 UTC] USER INFO::::
[07-Apr-2021 14:26:04 UTC] root
[07-Apr-2021 14:26:04 UTC] apache
I am calling this at the moment from a simple php page called at http:///test_log.php:
<?php
require("/var/www/html/myproject/app/lib/log_fns.php");
//phpinfo();
my_logger("This is a test message from test_log.php");
?>
I tried to chmod the files to 777 in the ./lib directory but was still getting this error. I have hard coded the path you can see above creating the ulogs directory.
I have tried combinations of chmod the ulogs directory to 777 and chown the directory to apache:apache but still get this error.
Any ideas where I can look next or how to solve?
TIA
Run this command
sudo chmod 777 /var/www/html/myproject/ulogs/
With this work you file_put_contents can generate or edit the file in this directory
This happened to me and no changing of file/folder permissions worked. The issue in the end was SELinux, which is setup as enforcing by default on CentOS.
To disable it, edit /etc/selinux/config, and change
SELINUX=enforcing
to
SELINUX=disabled
Reboot and it should work.

How write in files through TYPO3 Command Controller

I wanna create files and write in log file through a TYPO3 command controller. I have used command controller, this is registered in ext_localconf as $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = VENDOR\ExtName\Command\GeneradorArchivosEncuestasCommandController::class;; the objetctive of this command controller is to create all templates in xlsx of a surveys of my extension domain, but in the mopment to create a file or write inside log files, I have the next error: failed to open stream: Permission denied.
Otherwise, the directories has 775 permissions and owner apache as group; and the users: _cli_lowlevel and _cli_scheduler, those user have the same file mount in BE referenced to typo3temp.
I am using TYPO3 7.6, PHP 5.6, Apache 2.2.15

Volt directory can't be written

The error I am getting is
Warning: Phalcon\Mvc\View\Engine\Volt\Compiler::compileFile(../app/views/index/index.phtml.php): failed to open stream: Permission denied in /Users/mattstephens/Sites/magpie/public/index.php on line 26 Phalcon Exception: Volt directory can't be written
I have declared the volt engine usage in my bootstrap like so
$view->registerEngines(array(
'.phtml' => 'Phalcon\Mvc\View\Engine\Volt'
));
The mention of line 26 in my code points to the application handle function shown below
echo $application->handle()->getContent();
Is this a permissions related thing or due to a missing directory?
Unless you specify a different folder for Volt to compile its templates, the folder where the view file is located will be used to create the relevant compiled file.
You can change this behavior by setting the proper option when you register your service as such:
use \Phalcon\Mvc\View as PhView;
use \Phalcon\Mvc\View\Engine\Volt as PhVolt;
...
public function initView($options = array())
{
$config = $this->di['config'];
$di = $this->di;
$this->di['volt'] = function ($view, $di) use ($config) {
$volt = new PhVolt($view, $di);
$volt->setOptions(
array(
'compiledPath' => $config->app_volt->path,
'compiledExtension' => $config->app_volt->extension,
'compiledSeparator' => $config->app_volt->separator,
'stat' => (bool) $config->app_volt->stat,
)
);
return $volt;
};
/**
* Setup the view service
*/
$this->di['view'] = function () use ($config, $di) {
$view = new PhView();
$view->setViewsDir($config->app_path->views);
$view->registerEngines(array('.volt' => 'volt'));
return $view;
};
}
The $config will store all the information you need. By using the compiledPath you instruct Volt to compile the templates there and then serve them to the front end. That folder needs to be writeable for the user that runs your web server www-data or other and can be outside your public folder.
The file structure I usually use is:
app
\controllers
\models
\views
public
\js
\css
\img
var
\volt
\logs
\config
\cache
Change volt file permission (Inside app/cache) to 777 .Its working fine
Contrary to several of the other answers here, don't just willy-nilly set permissions to 0777 and pretend everything's okay, that's completely ridiculous. Your server needs to write to the volt folder inside your cache directory.
You might need to create the folder first.
sudo mkdir cache, and sudo mkdir cache/volt. Then chown that folder with whatever is the username that your server runs.
NOTE: Depending on your configuration, the cache folder might be located at the project root and not inside app.
For example, if your server launches under the permissions of user named 'www-data' (which is most common), after creating the proper folder structure, the following command will fix your issue:
sudo chown www-data:www-data -R cache
I have just made "app/cache" folder with 777 permitions) It works))
change the "app/cache" folder permission to 777.
in my mac i did like this.
navigate inside the app folder of our phalcon framework
[chmod ugo=rwx cache]
make sure the cache folder permission is 777 by typing [ls -l].
Changing the app/cache and app/cache/volt with these commands worked for my project structure.
chmod -R a+w app/cache
chmod -R a+w app/cache/volt
Just go to /project_name/store/app/config/config.php and change cacheDir from 'cacheDir' => __DIR__ . '/../../app/cache/' to 'cacheDir' => __DIR__ . '/../../cache/'.
Also, change its write permission to 777.
You should change ownership of your project directory with chown command,
chown -R your_system_user:webserver_user yourproject/
Check if the baseUri in the config.php is same as the installation directory
if the issue persist, tail nginx logs tail -f /var/log/nginx/*.log.
Also try use sudo setenforce 0. This worked for me.

Kohana Logs file writable but permission denied

I have checked the install.php and it gives me the green light on the logs file. However, my application gets an error saying
ErrorException [ 2 ]: file_put_contents(/my/app/path/logs/2013/03/04.php): failed to open stream: Permission denied ~ SYSPATH/classes/Kohana/Log/File.php [ 90 ]
I am also faced the same issue,i solved it with help of below comment to make all sub directories writable.
chmod -R 777 cache/ logs/
This may help future referrer.
Even if logs/ directory exists and have proper permissions set, it's subdirectories (like logs/2013/ and logs/2013/03) could have improper permissions, for example due to some file copying or so...
Be sure that all subdirectories allow writting for user/group/other.

Categories