file_put_contents - failed to open stream: Permission denied

前端 未结 14 1522
耶瑟儿~
耶瑟儿~ 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:25

    This can be resolved in resolved with the following steps :

    1. $ php artisan cache:clear
    
    2. $ sudo chmod -R 777 storage
    
    3. $ composer dump-autoload
    

    Hope it helps

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

    this might help. It worked for me. try it in the terminal

    setenforce 0

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

    Gathering info from this link stackoverflow-image save doesn't work with chmod 777 and from user azerafati and Loek Bergman

    if you were to look under /etc/apache/envvars file you will see something like:

    export APACHE_RUN_USER=www-data
    export APACHE_RUN_GROUP=www-data
    

    Apache is run under the username 'www-data'

    '0755' means the file owner can read/write/execute but group and other users cannot write. so in ur terminal, cd to the folder containing your 'images' folder. then type:

    find images -type d -exec chmod 0755 {} \;
    find images -type f -exec chmod 0755 {} \;
    sudo chown -R www-data:www-data images
    

    you must change persmissions first before changing owner. enter your password when prompted. this will make 'www-data' owner of the images folder.

    your upload should now work.

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

    Guys I had this problem for 1 month and did everything but couldn't fix it, but now I know the solution.

    I use a shared linux hosting, when my admin changed the php to 5.3 I got many error for the "file_put_contents" code. try to test my plan:

    In your host create a file like mytest.php, and put this code in and save:

    <?php        mail('Your-EMail','Email-Title','Email-Message');        ?>
    

    Open the URL "www.your-domain.com/mytest.php" one time and then check your email. you should have an email from your host with the information you entered in mytest.php, check the sender name. if its from Nobody you have problem about "Permission Denied" because something not defined and if the sender name is like my id: iietj8qy@hostname5.netly.net you dont have prob.

    My admin changed the server and installed the host again I think and the problem got solved, tell your host administration what I told you and maybe they find the answer.

    hope it helps you!

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

    I know that it is a very old question, but I wanted to add the good solution with some in depth explanation. You will have to execute two statements on Ubuntu like systems and then it works like a charm.

    Permissions in Linux can be represented with three digits. The first digit defines the permission of the owner of the files. The second digit the permissions of a specific group of users. The third digit defines the permissions for all users who are not the owner nor member of the group.

    The webserver is supposed to execute with an id that is a member of the group. The webserver should never run with the same id as the owner of the files and directories. In Ubuntu runs apache under the id www-data. That id should be a member of the group for whom the permissions are specified.

    To give the directory in which you want to change the content of files the proper rights, execute the statement:

    find %DIR% -type d -exec chmod 770 {} \;
    

    .That would imply in the question of the OP that the permissions for the directory %ROOT%/database should be changed accordingly. It is therefor important not to have files within that directory that should never get changed, or removed. It is therefor best practice to create a separate directory for files whose content must be changed.

    Reading permissions (4) for a directory means being able to collect all files and directories with their metadata within a directory. Write permissions (2) gives the permission to change the content of the directory. Implying adding and removing files, changing permissions etc.. Execution permission (1) means that you have the right to go into that directory. Without the latter is it impossible to go deeper into the directory. The webserver needs read, write and execute permissions when the content of a file should be changed. Therefor needs the group the digit 7.

    The second statement is in the question of the OP:

    find %DOCUMENT_ROOT%/database -type f -exec chmod 760 {} \;
    

    Being able to read and write a document is required, but it is not required to execute the file. The 7 is given to the owner of the files, the 6 to the group. The webserver does not need to have the permission to execute the file in order to change its content. Those write permissions should only be given to files in that directory.

    All other users should not be given any permission.

    For directories that do not require to change its files are group permissions of 5 sufficient. Documentation about permissions and some examples:

    https://wiki.debian.org/Permissions

    https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions

    http://www.linux.org/threads/file-permissions-chmod.4094/

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

    There 2 way to resolve this issues
    1. use chmod 777 path-to-your-directory.
    if it does not work then
    2. simply provide the complete path of your file query.txt.

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