6

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!

2

1 Answer 1

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

while($row=fgets($fileData)){  
    // can parse further $row by usingstr_getcsv
    echo $row."\n";
   }
Sign up to request clarification or add additional context in comments.

3 Comments

How about fgetcsv()?
fgetcsv parse csv line, returns comma separated values as array, but @user3058657 wanted to read full line.
And close the file fclose($fileData); after the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.