Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP read CSV file line by line

Tags:

loops

php

csv

I have a CSV file contains a list of URLs that I want to download images. So the question is how to read CSV file line by line and run my code on each of URL.

I have links.csv file

url1.com
url2.com
url3.com

Here is my code that I want to run

$results_page = curl($url);

$img_url = scrape_between($results_page, "\"mainUrl\":\"", "\",\"dimensions\"");

$title = scrape_between($results_page, "<span id=\"productTitle\" class=\"a-size-large\">", "</span>"); 
$find = array('/ /', '/:/');
$replace = array('_', '');
$img_name = preg_replace($find, $replace, $title);

$data = curl($img_url);
file_put_contents('images/'.$img_name.'.jpg', $data);

Thank you!

like image 401
user3058657 Avatar asked Apr 02 '14 10:04

user3058657


1 Answers

$fileData=fopen($file,'r');

while($row=fgets($fileData)){  
    // can parse further $row by usingstr_getcsv
    echo $row."\n";
   }
like image 144
murtza gondal Avatar answered Oct 21 '22 06:10

murtza gondal