2013-05-27 27 views
-2

我有一個示例代碼:錯誤從URL使用捲曲獲取圖像?

admin 
-index.php 
uploads 

中的index.php我使用代碼:

function saveImageFromUrl($my_img, $fullpath){ 
    if($fullpath!="" && $fullpath){ 
     $fullpath = $fullpath."/".basename($my_img); 
    } 
    $ch = curl_init ($my_img); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $rawdata=curl_exec($ch); 
    curl_close ($ch); 
    if(file_exists($fullpath)){ 
     unlink($fullpath); 
    } 
    $fp = fopen($fullpath,'x'); 
    fwrite($fp, $rawdata); 
    fclose($fp); 
} 
$dir = dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR; 
$fullpath = "uploads"; 
saveImageFromUrl('image.png', $dir.$fullpath); 

,並導致錯誤得到圖像失敗=>如何解決呢?

+0

當您在最後一行調用'saveImageFromUrl'時,您提供的$ my_img不是有效的URL。另外,如果你在最後回顯$ dir。$ fullpath;',結果是一個有效的路徑嗎?爲什麼使用多個'dirname()'? –

回答

0
//$dir = dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR; 
$fullpath = "../uploads"; 
saveImageFromUrl('image.png', $fullpath);