[FIXED] Getting an error when I try to download images from a url with CURL

Issue

Hello everyone i’m kinda new to PHP and I want to try and download an image URL into a file on my taskbar. I found a small code on stackoverflow that let’s me do this but when I try it out I get the error fopen(): failed to open stream: Permission denied in. I looked on the internet about this error code and it said like that I had to give it permission to able to save the pictures in the file, but I don’t really know on how to that.This is the code that I have found:

function saveImage(){
    $imageFolder = "C:\Users\kemal\OneDrive\Bureaublad\Parfume";
    $ch = curl_init('https://static.wikia.nocookie.net/dragonball/images/e/e4/Goku_arrives_colored.PNG/revision/latest?cb=20210204093245');
    $fp = fopen($imageFolder, 'w');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}

these are the errors that i’m getting
This is the error that i'm getting

also tried to use this code instead of curl, but I get the same error(permission denied):

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));

Solution

I’m assuming you’re running php on Apache HTTP server.

First thing I’d try is to set your $imageFolder path to a folder within the apache2/htdocs folder as the process should have write access to that. If that works then you’d need to set permissions on the original target folder to include the user that the apache process is running under. For obvious reasons webservers are normally fairly restricted as to what folders they can read and write to.

For the second example ($img = ‘/my/folder/flower.gif’), you’ll probably find that path doesn’t exist.

Answered By – AdamUK73

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published