我沒有安裝捲曲。 已經提到過這個代碼應工作,從網址抓取圖片並保存,大小爲0字節
file_put_contents($target_path,file_get_contents($image));
,但它不適合我,它把圖像與正確的名稱等...但與0字節的大小。 $ target_path具有正確的權限,並且allow_url_fopen處於On狀態。
我做錯了什麼?
我沒有安裝捲曲。 已經提到過這個代碼應工作,從網址抓取圖片並保存,大小爲0字節
file_put_contents($target_path,file_get_contents($image));
,但它不適合我,它把圖像與正確的名稱等...但與0字節的大小。 $ target_path具有正確的權限,並且allow_url_fopen處於On狀態。
我做錯了什麼?
allow_url_fopen
不是唯一標準,因爲URL上的file_get_contents
可能會中斷。服務器可能被設置爲處理或檢測授權/用戶代理等。
首先嚐試將您的數據放入一個變量並打印出來,看看它是否能夠獲取內容;
$img = "";
$img = file_get_contents($image);
echo $img; //for debugging..
//for running..
if(!$img) die("no data fetched");
現在,如果$img
有數據,接下來嘗試將其寫入文件:
$result = file_put_contents($target_path,$img);
if($result=== FALSE)
die("Error writing data into $target_path");
else
echo "$result bytes written to $target_path";
你所面臨的問題是,有你有一個嵌套函數列表多個故障點。雖然它很好,並且將多行代碼壓縮成一行代碼,但很難調試,而且在遇到類似您所面臨的代碼時,您將無法輕鬆確定哪一個代碼是有問題的。
好吧,它給了我這個,20418字節寫到/storage/.../..../images/(我修改了這個路徑,以防萬一這個演示),但是那個文件夾裏沒有文件,文件doesn不會被抄襲。 :( – 2013-04-29 19:49:48
@JohanLarsson'/storage/.../..../images/'有問題,沒有文件名,給出完整的路徑和文件名 – raidenace 2013-04-29 19:50:35
好的,這是問題所在,非常感謝,謝謝你的其他身體。 – 2013-04-29 19:52:22
它必須工作檢查$image
和$target_path
的內容,路徑和URL是正確的還是不錯的使用dirname(__FILE__)
爲$target_path
像dirname(__FILE__).'/image.jpg'
它有助於有正確的目標路徑
注意您必須使用完整路徑$target_path
你可以測試這個功能,它也可以與fopen一起工作,但速度較慢,但速度較慢
function Request($url,$File="",$Method='POST',$data=null, $optional_headers = null,$Debug=0){
$params = array('http' => array('method' => $Method));
$optional_headers.="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$optional_headers.="Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n";
$optional_headers.="Accept-Encoding:gzip,deflate,sdch\r\n";
$optional_headers.="Accept-Language:en-US,en;q=0.8\r\n";
$optional_headers.="Cache-Control:max-age=0\r\n";
$optional_headers.="Connection:keep-alive\r\n";
$optional_headers.="User-Agent:Mozilla/5.0 AppleWebKit/536.5 (KHTML, like Gecko) SepidarBrowser/1.0.100.52 Safari/536.5\r\n";
if ($data !== null) {
$params['http']['content'] = $data;
}
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
return false;
}
$response= @stream_get_meta_data($fp);
$out['header'] = $response['wrapper_data'];
$out['body']='';
if($File!=""){
$fout = @fopen($File, 'w+');
}
while([email protected]($fp)){
[email protected]($fp,1024*8);
// echo "***************\n".strlen($buffering)."\n".$buffering."\n***********************";
if($buffering==''){break;}
if($File!=""){
@fwrite($fout,$buffering);
if($Debug==1)echo strlen($buffering)."-->Download And Stored IN".$File;
}
$out['body'] .=$buffering;
}
if(trim(@$out['header']['Content-Encoding'])=='deflate'){
$out['body']=gzinflate($out['body']);
}
fclose($fp);
return $out;
}
Request($target_path,$image,'GET');
嘗試了分裂你的代碼,並添加一些基本的錯誤處理,這樣可以或許做財產以後,而不是僅僅寫0字節到一個文件中的錯誤的情況下:
<?php
error_reporting(E_ALL);
$target_path = "./some_path/";
$new_img = "new_image.jpg";
if(file_exists($target_path)){
$img = file_get_contents($image);
if(strlen($img) >=1){
file_put_contents($target_path . $new_img, $img);
}else{
echo 'Error fetching $image';
}
}else{
echo '$target_path not found';
}
?>
是'$ image'文件路徑? – 2013-04-29 19:31:05
'file_get_contents($ image)'實際上取什麼東西?它會引發錯誤嗎?嘗試調試它的部分,而不是整體。 – pilsetnieks 2013-04-29 19:31:40
看看[這個答案](http://stackoverflow.com/questions/16031076/zend-pdf-display-a-url-qrcode/16031427#16031427),它可能會爲您提供一個解決方案;-) – Havelock 2013-04-29 19:33:17