R
R
robert_n2016-02-08 13:24:20
PHP
robert_n, 2016-02-08 13:24:20

How to upload an image to your server?

Hello!
I am writing authorization through Google and it became necessary to upload the user's avatar to my server.
Google sends me a response that has a link to the avatar:

"picture": "https://lh6.googleusercontent.com/-sdKkMmClA5M/BABBBBBBDI/AAAAABAAAQg/4SjnUI7UOsQ/s96-c/photo.jpg"

I'm uploading her trail. way
$picture = $token_info['picture'];
$sourcecode=GetImageFromUrl($picture);
$savefile = fopen('/var/www/html/uploads/avatars/image.jpg', 'w');
fwrite($savefile, $sourcecode);
fclose($savefile);

function GetImageFromUrl($link) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
return $result;
}

When uploading, an error occurs in the logs:
fopen(): failed to open stream: Permission denied
Apparently there are problems with access rights to the folder where I download the file. I read this article and added the www-data user to my group, which I assigned the rights to:
sudo usermod -a -G user_main www-data
sudo chmod -R 750 /var/www/html/uploads/avatars/

But after doing that, the server now throws a 403 error:
You don't have permission to access /upload_avatar on this server.

Advise how to properly upload files to your server. Maybe there is an article or an example?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2016-02-08
@robert_n

I don’t know how you have everything set up there, but you can try to do the following:

  • Allow access to your server files
    Open the apache2.conf file located at
    Locate the line "Directory /var/www/" and write the following
    <Directory /var/www/>
      Options Indexes FollowSymLinks
      AllowOverride All	
      Allow From All
      Require all granted
    </Directory>

    Restarted Apache
    Everything should work
  • R
    Ruslan Ibragimov, 2016-02-08
    @durachook

    fopen(): failed to open stream: Permission denied
    is an error due to the fact that Google gives a 404 error on the link to the photo, Not Found (image not found)
    and then you have to play with the rights, put 777 for sure)

    M
    Mikhail Osher, 2016-02-08
    @miraage

    stackoverflow.com/questions/724391/saving-image-fr...
    If that doesn't work, it's obviously a permissions issue. This is already a separate issue.

    Didn't find what you were looking for?

    Ask your question

    Ask a Question

    731 491 924 answers to any question