2016-03-04 56 views
0

我需要從外部鏈接獲取圖像文件並存儲到本地文件夾中。從外部鏈接獲取文件並存儲到本地服務器

我已經使用以下:

$value1 = "https://images.airlist.com/0008459/Listing/129128/3350520160227084506.JPG%3Fv=1?v=1"; 

$img_file = file_get_contents(str_replace("?v=1","",$value1)); 
$file_handler = fopen($file_loc,'w'); 
if(fwrite($file_handler,$img_file)== false) 
    { 
      echo 'error'; 
    } 
    fclose($file_handler); 
    } 

圖像被存儲在一個格式,如:

3308420160227084509.JPG%3Fv = 1-50x50.jpg%3fv = 1

+0

使用'file_put_contents(目的地的file_get_contents(URL))' – treegarden

+1

它是你的實際代碼?您應該在網址附近添加一些引號... – PeterPan666

+0

這裏存儲的圖像類似於(2997720160227084507.JPG%3Fv = 1);不支持的格式。無法打開文件。 – soniya

回答

0

我會做這樣的事情:

$url = "https://images.airlist.com/0008459/Listing/129128/3350520160227084506.JPG%3Fv=1?v=1"; 
$file = explode('?', $url)[0]; // get everything on the left site from the ? 
$file = explode('%', $file)[0]; // get everything on the left site from % 
$filename = basename($file); // get only the filename 
$fileContent = file_get_contents($url); // get image content 
file_put_contents($filename, $fileContent); // write image content to filename 
+0

如果我想刪除%3Fv = 1。沒有圖像從鏈接返回。 – soniya

+0

當我刪除%3fv = 1。我得到了指定的blob不存在。 RequestId:71bb7efd-0001-001b-140c-7656a5000000時間:2016-03-04T11:56:19.2772739Z。 – soniya

+0

我糾正我的答案,並給你一個完整的代碼 – Candyman1332

0

你一小時前沒有回覆我的評論,但我會假設你想下載https://images.airlist.com/0008459/Listing/129128/3350520160227084506.JPG%3Fv=1‌​?v=1保存它作爲3350520160227084506.JPG

<?php 

$value1 = "https://images.airlist.com/0008459/Listing/129128/3350520160227084506.JPG%3Fv=1?v=1"; 

// Take the basename (3350520160227084506.JPG%3Fv=1?v=1) 
// And remove anything starting from either a percent sign (%) or a question mark (?) 
$fileName = preg_replace('~[%?].*$~', '', basename($value1)); 

// $fileName = string(23) "3350520160227084506.JPG" 

file_put_contents($fileName, file_get_contents($value1)); 
相關問題