PHP newbie CURL and array

UPDATED: I've simplified the code (tried to)

I'm trying to download a series of images as set in an array, but something is clearly not right:

function savePhoto($remoteImage,$fname) {
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_NOBODY, true);
    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($retcode==200) {
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, ".{$fname}.jpg",100);
    }
    return $retcode;
}

$filesToGet = array('009');
$filesToPrint = array();

foreach ($filesToGet as $file) {
        if(savePhoto('http://pimpin.dk/jpeg/'.$file.'.jpg',$file)==200) {
            $size = getimagesize(".".$file.".jpg");
            echo $size[0] . " * " . $size[1] . "<br />";
        }
}

I get the following errors:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 15

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 16

Warning: getimagesize(.009.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\inetpub\vhosts\dehold.net\httpdocs\ripdw\index.php on line 26 *

Answers


try this instead:

function get_file1($file, $local_path, $newfilename)
{
    $err_msg = '';
    echo "<br>Attempting message download for $file<br>";
    $out = fopen($newfilename, 'wb');
    if ($out == FALSE){
      print "File not opened<br>";
      exit;
    }

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_FILE, $out);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $file);

    curl_exec($ch);
    echo "<br>Error is : ".curl_error ( $ch);

    curl_close($ch);
    //fclose($handle);

}//end function 

// taken from: http://www.weberdev.com/get_example-4009.html

or file_get_contents


You should try file_get_contents in replacement of CURL (simpler but it does the job):

 function savePhoto($remoteImage,$fname) {        
      $fileContents = file_get_contents($remoteImage);        
      try {        
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, ".{$fname}.jpg",100);        
      } catch (Exception $e) {        
        //what to do if the url is invalid        
      } 
}

I finally got it to work, with help from you all and a bit of snooping around :-)

I ended up using CURL:

function savePhoto($remoteImage,$fname) {
    $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);

    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);
    if($retcode == 200) {
        $newImg = imagecreatefromstring($fileContents);
        imagejpeg($newImg, $fname.".jpg",100);
    }

    return $retcode;
}


$website = "http://www.pimpin.dk/jpeg";
$filesToGet = array('009');
$filesToPrint = array();

foreach ($filesToGet as $file) {
        if(savePhoto("$website/$file.jpg",$file)==200) {
            $size = getimagesize($file.".jpg");
            echo $size[0] . " * " . $size[1] . "<br />";
        } else {
            echo "File wasn't found";
        }
}

Need Your Help

Load scrollable div at bottom

javascript html css angularjs

I'm developing a chat in javascript (angularjs without jQuery), and I would like to know if it's possible to load a scrollable div to the bottom.

ASP.NET localization--where are the international resource strings?

asp.net localization

I just started working on a new International project. All strings in the markup of ASPX pages are instead System.Web.UI.WebControls.Localize controls with "meta:resourcekey=" attributes. In the we...