2010-07-08 24 views
1

更新:我已經簡化了代碼(試圖)PHP新手卷曲和陣列

我想作爲一個陣列設置下載的一系列圖像,但事情顯然不是正確的:

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 />"; 
     } 
} 

我得到以下錯誤:

警告:imagecreatefromstring() [function.imagecreatefromstring]:在 ç 空字符串或無效圖像:\ inetp UB \虛擬主機\ dehold.net \的httpdocs \ ripdw \的index.php 第15行

警告:imagejpeg():提供 參數不是 ℃的有效的圖像資源 :\的Inetpub \虛擬主機\ dehold。淨\的httpdocs \ ripdw \的index.php 第16行

警告:和getimagesize(.009.jpg) [function.getimagesize]:未能 打開流:在 C無這樣的文件或目錄 :\的Inetpub \虛擬主機\ dehold.net \的httpdocs \ ripdw \的index.php 上線26 *

+0

問題是什麼? – Sjoerd 2010-07-08 10:52:18

+0

任何錯誤信息? – Artefacto 2010-07-08 10:53:07

+0

大家好!我已經更新的問題:-) 這是我在這裏的第一Q堆棧,所以我希望你多多包涵:) – kasperwf 2010-07-08 13:59:40

回答

0

試試這個:

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 

//摘自:http://www.weberdev.com/get_example-4009.html

file_get_contents

+0

我想你的建議,但它並沒有將文件保存爲JPEG,和我真的喜歡讓我的代碼工作(並希望瞭解我做錯了什麼):-) 感謝您的輸入 – kasperwf 2010-07-08 14:01:12

0

你應該嘗試在更換捲曲的file_get_contents(簡單,但它的工作):

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   
     } 
} 
+0

這有助於保存文件,但它會返回一個錯誤,如果該文件不存在而404則返回: ... 警告:的file_get_contents(http://pimpin.dk/jpeg/00x9.jpg)function.file-GET-內容]:未能打開流:HTTP請求失敗! HTTP/1.1 404用C未找到:\的Inetpub \虛擬主機\ dehold.net \的httpdocs \ ripdw \的index.php上線22 ... 是否可以預防呢? – kasperwf 2010-07-09 07:31:29

+0

是的,把你的$ fileContents = file_get_contents($ remoteImage);在嘗試。 如果它失敗了(如果是404),那麼你的catch指令將被處理而不會拋出Warning(除非你在catch中拋出異常,顯然)。 – bPizzi 2010-07-09 10:00:35

0

I F inally得到它的工作,並得到您的幫助,所有的,有點周圍窺探:-)

的我結束了使用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"; 
     } 
}