Home > Enterprise >  i can't download an image from url php
i can't download an image from url php

Time:09-30

I'm at a dead end, I have a url of a photo that I can't download it.

Url not working:

$imageUrl = 'https://www.casaonlineitalia.it/immobili/241/vendita_rusticocasale_capannori_san_ginese_di_compito_capannori_lu_1950039608648520915.jpg';
@$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents(__DIR__ .'/img/foto1.jpg',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}

using another url all this works

url working :

$imageUrl = 'https://static3.agimonline.com/images/4Zzoy0MDM1fO2M65MTAw5NDYxeO2lkcOjE43NTEw9NTI70ZXh0fOmpw5Zzt25OjE7edzoxc/r/0/1851052.jpg';
@$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents(__DIR__ .'/img/foto1.jpg',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}

Do you have an explanation or solution?

Out of curiosity I tried to download the image through websites that allow you to download images from a url. They also give me error and won't let me download the image. I don't know if it's a problem with my server, or with the server hosting the image.

Ulteriore codice testato:

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);    

$imageUrl = 'https://www.casaonlineitalia.it/immobili/241/vendita_rusticocasale_capannori_san_ginese_di_compito_capannori_lu_1950039608648520915.jpg';           
    $arrContextOptions=array(

        "ssl"=>array(

            "verify_peer"=>false,

            "verify_peer_name"=>false,    
        ),   
    );   
    $rawImage = file_get_contents($imageUrl, false, stream_context_create($arrContextOptions));    
    if($rawImage)
        {    
        $directory = "img";    
        if(!is_dir($directory)) {    
            mkdir("img", 0775, true);    
        }    
        file_put_contents($directory . '/foto2.jpg', $rawImage);    
        echo 'Image Saved';   
    }    
    else   
    {   
        echo 'Error Occured';   
    }
 ?>

CodePudding user response:

you are not validating the ssl og the remote image. Ignore the peer verification to test:

$imageUrl = 'https://www.casaonlineitalia.it/immobili/241/vendita_rusticocasale_capannori_san_ginese_di_compito_capannori_lu_1950039608648520915.jpg';

    $arrContextOptions=array(
        "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );

    $rawImage = file_get_contents($imageUrl, false, stream_context_create($arrContextOptions));
    if($rawImage)
    {
        $directory = "img";
        if(!is_dir($directory)) {
            mkdir("img", 0775, true);
        }
        file_put_contents($directory . '/foto1.jpg', $rawImage);
        echo 'Image Saved';
    }
    else
    {
        echo 'Error Occured';
    }

CodePudding user response:

both codes are working.check folder permissions(w) of /img folder for which code is not working.

  • Related