Search code examples
phpimagegoogle-cloud-platformchmod777

Image Upload Error due to permission denied to write


I have an upload image feature in my website. I tried it in my localhost and it was working fine. Now I hosted it, and the following error occured:

move uploaded file failed to open stream permission denied

I checked the error online and found that by changing the chmod to 777, it can be solved.

I tried the same, and it worked. Even just 713 is working fine. But giving 777 permission is not good due to hacking possibilities.

Is there a way around this? I have right now changed the chmod to 713 until I can get a better solution for the same.

I have currently hosted in Google Cloud Platform with Ubuntu 16.04 image


Solution

  • You can use 2 options.

    1) you can change the owner from the upload directory to the apache user

    chmod www-data:www-data your_folder
    

    but this has some negative effects. your ssh user can edit files here just with root privileges.

    2) you can create a group only for upload files and put your ssh user and the apache user into this group

    sudo addgroup uploaders
    sudo usermod -aG uploaders www-data
    sudo usermod -aG uploaders your_user
    

    after this change the owner of the upload folder

    chmod your_user:uploaders your_folder
    

    after this you can change the permissions to 744 or something you want