現在:my codeは、csvファイルで指定されたURLの画像を保存します。csv値に基づいてURLから保存されたphpの名前変更イメージ
目標:同じ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
私は私が正しく理解していない:100A1_https 'に// www.instagram.com/P/BBzUXUFLrGH /'::あなたは、HTTPS 'からフェッチされたファイルの名前を変更しようとしている// www.instagram.com/p/BBzUXUFLrGH/_48.8486557_2.3481125'? –
。 csvとは何の関係もないランダムなコンピュータ生成の命名規則(img_1など)によって画像が保存されています。 – eho