file_put_contents - failed to open stream: Permission denied

前端 未结 14 1524
耶瑟儿~
耶瑟儿~ 2020-11-27 03:05

I am trying to write a query to a file for debugging. The file is in database/execute.php. The file I want to write to is database/queries.php.

相关标签:
14条回答
  • 2020-11-27 03:37

    If you are pulling from git from local to server, you will need to clear cache sometimes because of the view files it gets uploaded with it / or other cached files .

    php artisan cache:clear
    

    Sometimes it might just to the trick if your application was working before the git pull

    0 讨论(0)
  • 2020-11-27 03:39

    Furthermore, as said in file_put_contents man page in php.net, beware of naming issues.

    file_put_contents($dir."/file.txt", "hello");
    

    may not work (even though it is correct on syntax), but

    file_put_contents("$dir/file.txt", "hello");
    

    works. I experienced this on different php installed servers.

    0 讨论(0)
  • 2020-11-27 03:44

    Realise this is pretty old now, but there's no need to manually write queries to a file like this. MySQL has logging support built in, you just need to enable it within your dev environment.

    Take a look at the documentation for the 'general query log':

    http://dev.mysql.com/doc/refman/5.1/en/query-log.html

    0 讨论(0)
  • 2020-11-27 03:44

    For anyone using Ubuntu and receiving this error when loading the page locally, but not on a web hosting service,

    I just fixed this by opening up nautilus (sudo nautilus) and right click on the file you're trying to open, click properties > Settings > and give read write to 'everyone else'

    0 讨论(0)
  • 2020-11-27 03:45

    The other option

    is that you can make Apache (www-data), the owner of the folder

    sudo chown -R www-data:www-data /var/www
    

    that should make file_put_contents work now. But for more security you better also set the permissions like below

    find /var/www -type d -print0 | xargs -0 chmod 0755 # folder
    find /var/www -type f -print0 | xargs -0 chmod 0644 # files
    
    • change /var/www to the root folder of your php files
    0 讨论(0)
  • 2020-11-27 03:51

    had the same problem; my issue was selinux was set to enforcing.

    I kept getting the "failed to open stream: Permission denied" error even after chmoding to 777 and making sure all parent folders had execute permissions for the apache user. Turns out my issue was that selinux was set to enforcing (I'm on centos7), this is a devbox so I turned it off.

    0 讨论(0)
提交回复
热议问题