Save a website image with PHP?

Asked

Viewed 675 times

0

How I save an image of a particular site to a folder on my site using PHP?

And how would you look to resize that image?

I used this way but it didn’t work: file_put_contents($directory, file_get_contents($url)); also tried with the copy function and got no result..

1 answer

4


His attempt with file_get_contents only works if the php.ini is set this way:

allow_url_fopen=true

But if you’re qualified, you don’t even need file_get_contents, can already simplify with copy:

copy( $urlOrigem, $arquivoDestino );


If you do not wish or cannot activate allow_url_fopen, an alternative is the cURL:

$curl = curl_init( $urlOrigin );
$file = fopen( $fileDestination, 'wb' );
curl_setopt( $curl, CURLOPT_FILE, $file );
curl_setopt( $curl, CURLOPT_HEADER, 0 );
curl_exec( $curl );
curl_close( $curl );
fclose( $file );

In this second case, the extension cURL has to be enabled.


About resizing image with PHP, we already have some answers on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.