0

I'm trying to write a script that will allow for a two column .csv file (1st column with names, 2nd with image urls) to be read, and the images to then be saved to my hard drive using the first column as the saved file name. I'm able to get some images to save (all the files are created, but some are missing data). The naming convention works the way I want it to, but I need to get all of the files. Anyone care to take a look? Might be worth mentioning that I'm using MAMP but the image addresses in the .csv are all valid (I echoed out the image address with tags, so I know they work).

<?php
    //header('Content-Description: File Transfer');
    //header('Content-Disposition: attachment; filename ='
    //Eli's 2 column CSV grab-n-save file

    $file = 'Random.csv'; //put file path of .csv here
    $buffer = file_get_contents($file);
    //print_r($buffer);
    $pattern = '/[,\n]/';

    $catch = preg_split($pattern, $buffer);
    $keep_track = 0;

    foreach($catch as $value)
    {
        if ($keep_track%2 == 0)
        {
        //if first column is sku
            $name = $value;
            $keep_track += 1;
        }
        elseif ($keep_track %2 == 1)
        {
            //if an image
            $name = str_replace(' ', '',$name);

            //Change pathname to MAMP specs
            $name2 =  '/Applications/MAMP/htdocs/picturegrab' . $name . '.jpg';

            file_put_contents($name2, file_get_contents($value), FILE_APPEND);
            //echo $value . '<br/>';
            $keep_track += 1;

            echo $name2 . '<br/>' . '<img src='.$value.'><br/>';
        }

    }
?>
koerbcm
  • 340
  • 1
  • 2
  • 13
Sam Green
  • 15
  • 1
  • 4
  • 1
    what is your question ? – Raptor Jul 09 '13 at 13:42
  • can you add sample csv and your expected output ? – Baba Jul 09 '13 at 13:42
  • maybe read string by string with [fgetcsv](http://php.net/manual/ru/function.fgetcsv.php) is better – vladkras Jul 09 '13 at 13:43
  • My question is, how come some of the images are saving as the images they should be, yet others have zero bytes of data? It's completely random. My sample csv looks like this ------------------------------------------------------------ Name | Webaddress Jon Smith http://somewebsite.com/image.jpg Patty Jones http://somewebsite.com/image.jpg ------------------------------------------------------------- The files save on my computer as jonsmith.jpg & patty.jpg, but even though images display, they aren't saved – Sam Green Jul 09 '13 at 14:41

2 Answers2

0

some logic

$file = fopen('Random.csv');
$dir = '/Applications/MAMP/htdocs/picturegrab/';
while (list($name, $img_link) = fgetcsv($file)) {
  $name = str_replace(" ", "", $name);
  copy($img_link, $dir.$name.'.jpg');
}

updated my example to make it more responsive

also thx to Sam Green with his coment

vladkras
  • 16,483
  • 4
  • 45
  • 55
  • I'm no longer seeing anything. This is probably because it's not echoing, but the files aren't saved either. – Sam Green Jul 09 '13 at 18:26
  • 1
    Nevermind - this worked beautifully once I used $file = fopen('Random.csv'); Tanks a ton! – Sam Green Jul 09 '13 at 18:31
  • oh, sorry, I forgot that [fgetcsv()](http://php.net/manual/ru/function.fgetcsv.php) needs a resource of opened file, not the file +1 – vladkras Jul 09 '13 at 23:52
0
  1. Remove the whitespace before the opening php tab or else you will get junk whitespace and a headers already sent error will show up in the csv when you download it

  2. Remove the closing php tag as it is not necessary and will also help to prevent garbage whitespace Why do some scripts omit the closing PHP tag, '?>'?

  3. Use fgetcsv (http://php.net/manual/en/function.fgetcsv.php) to get the data

  4. Use fputcsv (http://php.net/manual/en/function.fputcsv.php) to write the data

Community
  • 1
  • 1
  • White space is removed, I haven't taken out the ?> though. I will try modifying the code shortly - thanks for the input! – Sam Green Jul 09 '13 at 18:18