PHP - rmdir (permission denied)

后端 未结 4 1549
挽巷
挽巷 2021-01-13 20:48

I have an easy script to create and delete a folder, but when I try to delete a folder, it brings up and error.

The code:



        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 21:27

    The webserver requires write access to the folder you're trying to delete. You can provide this with:

    chgrp -R www-data g_test/uploads
    chmod g+w g_test/uploads
    

    where www-data is the user that the webserver runs under (may be apache or some variation depending on your OS and server installation). After this you can run rmdir (or rm -r if the directory isn't empty).

    Also, keep in mind that giving the web server the ability to write to a directory posses security problems. In certain situations, this could allow a malicious user to run arbitrary code (i.e. take over your machine), or modify your website (i.e. server spyware).

    For these reasons, you should only give dirs write perms that:

    • absolutely need them
    • don't contain source code
    • are outside the dir containing scripts
    • owned by the server

    In this setup on a production machine, you could set up a separate directory just for this type of file, that only Apache can write to. If you have to deploy files to this directory, use sudo or the root account to do so to limit the accounts that have access.

    For a more complete description of what I mean, have a look at the security tips section in the Apache documentation.

提交回复
热议问题