Save image of a website with PHP?

-1

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

And how would you resize that image?

I used it this way but it did not work: file_put_contents ($ directory, file_get_contents ($ url)); I also tried with the copy function and did not get any result ..

    
asked by anonymous 03.01.2016 / 22:46

1 answer

4

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

allow_url_fopen=true

But if it is enabled, nor does it need file_get_contents , you can simplify with copy :

copy( $urlOrigem, $arquivoDestino );


If you do not want or can not activate allow_url_fopen , an alternative is 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 cURL extension must be enabled.


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

    
03.01.2016 / 23:26