2016-05-16 105 views
0

當前:我的代碼保存了csv文件中給出的URL的圖像。php根據csv值從url保存的圖像重命名

目標:重命名根據在同一csv文件的值保存的圖像,並通過格式:CSV位置3,4,5,6,7,8
(例如100A1_https://www.instagram.com /p/BBzUXUFLrGH/_48.8486557_2.3481125)。

我想將this stackoverflow示例合併到我的代碼中。但無法如我所希望的那樣迷路。

<?php 
 

 
$destination = 'images/'; 
 
$dom = new DOMDocument; 
 

 
$dir = "dir to photos"; 
 

 
$row = 0; 
 

 
if (($handle = fopen("data/testcsvfile.csv", "r")) !== FALSE) { 
 
    while (($data = fgetcsv($handle, 100, ",")) !== FALSE /*&& $row < 1*/) { 
 
    $row++; 
 

 
    // number of fields in each row 
 
    $num = count($data); 
 

 
    // get url from position in csv 
 
    $url = $data[6]; 
 
    echo $row . " " . $url. PHP_EOL; 
 

 
    if(strlen($url) > 0){ 
 
     // if we have a url, get the contents 
 
     $page = file_get_contents($url); 
 
     //echo $page; 
 

 
     $dom->loadHTML($page); 
 

 
     // find url using meta tag 
 
     $my_tags = $dom->getElementsByTagName('meta'); 
 

 
     // find which one is the image and grab and save 
 
     foreach ($my_tags as $tag) { 
 
      if($tag->getAttribute("property") == "og:image"){ 
 
       $image_url = $tag->getAttribute('content'); 
 
       echo $image_url . PHP_EOL; 
 
       $the_image = file_get_contents($image_url); 
 

 
       // rename file based on csv 
 
       // $newname = "$dir"."$names[3] $names[4] $names[5]"."; 
 
       rename($newname); 
 

 

 
       file_put_contents($destination . "img_" . $row . ".jpg", $the_image); 
 

 
      } 
 

 
     } 
 

 
    } 
 

 
    } 
 
    fclose($handle); 
 
} 
 

 
?>
// test csv file 
 

 
Mon,1,0,100,A,1,https://www.instagram.com/p/BBzUXUFLrGH/,48.8486557,2.3481125 
 
Mon,1,0,100,A,1,https://www.instagram.com/p/BAe0tGULrC1/,48.85272468,2.347259349 
 
Mon,1,0,100,A,1,https://www.instagram.com/p/_zik5YLrMf/,48.85356691,2.345645975

+0

我不知道我的理解正確:你想將文件從'的https牽強重命名:// www.instagram.com/p/BBzUXUFLrGH /''到100A1_https:// www.instagram.com/p/BBzUXUFLrGH/_48.8486557_2.3481125'? –

+0

。圖像目前正在通過隨機計算機生成的與csv無關的命名約定(例如img_1)進行保存。 – eho

回答

0

首先你要明白,使用URL作爲文件名是一個很奇怪的/糟糕的事情。 一個URL使用特殊字符如'/',這些字符在某些操作系統中是目錄分隔符。 如果你真的需要做到這一點,你需要先逃跑到文件名中的字符(或更好地與像下劃線非特殊字符替換它們):

$newname = str_replace('/', '\/', $newname); 

腳本的一個更新版本的安全替代用下劃線斜線:

<?php 

$destination = 'images'; 
$dom = new DOMDocument; 

if (($handle = fopen("data/testcsvfile.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 100, ",")) !== FALSE) { 
    $num = count($data); 

    // get url from position in csv 
    $url = $data[6]; 
    echo $row . " " . $url. PHP_EOL; 

    if(strlen($url) > 0){ 
     // if we have a url, get the contents 
     $page = file_get_contents($url); 
     //echo $page; 

     $dom->loadHTML($page); 

     // find url using meta tag 
     $my_tags = $dom->getElementsByTagName('meta'); 

     // find which one is the image and grab and save 
     foreach ($my_tags as $tag) { 
      if($tag->getAttribute("property") == "og:image"){ 
       $image_url = $tag->getAttribute('content'); 
       echo $image_url . PHP_EOL; 
       $the_image = file_get_contents($image_url); 

       $destinationName = str_replace('/', '_', sprintf(
        "%s%s%s_%s_%s_%s", 
        $data[3], 
        $data[4], 
        $data[5], 
        $data[6], 
        $data[7], 
        $data[8] 
       )); 

       $destinationPath = sprintf(
        "%s/%s", 
        $destination, 
        $destinationName 
       ); 


       file_put_contents($destinationPath, $the_image); 

      } 

     } 

    } 

    } 
    fclose($handle); 
} 

?> 
+0

很酷,謝謝!最終,我必須在地圖上繪製這些圖像,並讓每個圖像對應其各自的超鏈接。我不確定最好的方式去做。我想我可以放棄超鏈接並以不同的方式重新關聯它。 – eho

+0

我用功能代碼更新了答案。如果你不能使用數據庫(但是在這種情況下考慮sqlite),你可以使用其他字符替換斜槓,然後用原始斜槓重新獲得原始URL,以獲得原始URL –

+0

哇,這太棒了。非常感謝!我會看看sqlite。再次感謝! – eho