0

<?php
// output headers so that the file is downloaded rather than displayed
 header('Content-type: text/csv');
 header('Content-Disposition: attachment; filename="csv.csv"');
 
// do not cache the file
 header('Pragma: no-cache');
 header('Expires: 0');
 
// create a file pointer connected to the output stream
 $file = fopen('php://output', 'w');
 
// send the column headers
 fputcsv($file, array('Filepath', 'Folders', 'Date'));
 
// data
  
  $shopurl = "https://www.example.com/";
 
 $scanner = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
 
 $Fpath = array(); 
 $Folders = array(); 
 $Date = array(); 
 
  foreach ($scanner as $fileInfo) {
  
      if ($fileInfo->isDir()){ 
          continue;
      }
   
   $filepath = substr($fileInfo->getPathname(), 2);
   $cur_dir = explode('/', getcwd());
   
      $Fpath[] = $shopurl . $filepath;
      $Folders[] = substr(pathinfo($fileInfo->getPathname(), PATHINFO_DIRNAME), 2);
      $Date[] = date ("F d Y H:i:s.", filemtime($fileInfo));
  
  }

 $data = array($Fpath, $Folders, $Date);
 
 
// output each row of the data
 foreach ($data as $column)
 {
  fputcsv($file, $column);
 }
 
 exit();

With the code above I tried to get a csv with three columns. The output of the arrays look like:

Array
(
    [0] => Array
        (
            [0] => https://www.example.com/GHI/123/new.php
            [1] => https://www.example.com/ABC/123.php
            [2] => https://www.example.com/output.php
            [3] => https://www.example.com/csv.php
        )

    [1] => Array
        (
            [0] => GHI/123
            [1] => ABC
            [2] => 
            [3] => 
        )

    [2] => Array
        (
            [0] => April 12 2018 11:15:03.
            [1] => April 13 2018 10:28:30.
            [2] => April 13 2018 12:36:16.
            [3] => April 13 2018 12:32:10.
        )

)

So the problem right now is that all filepaths are displayed in the first array / row, the folder names are in second and date in last. To get the arrays correctly displayed in csv I need to have for each array / row the three values (filepath, folder, date).

I tried to get it done with array_flip but it doesn't work. Maybe someone has an advice. Thanks!

Rob
  • 59
  • 2
  • 2
  • 8

2 Answers2

2

You could try a different approach on $data stracture

    $data = [];

        foreach ($scanner as $key => $fileInfo) {

            if ($fileInfo->isDir()){ 
                continue;
            }

            $filepath = substr($fileInfo->getPathname(), 2);
            $cur_dir = explode('/', getcwd());

            $path = $shopurl . $filepath;
            $olders = substr(pathinfo($fileInfo->getPathname(), PATHINFO_DIRNAME), 2);
            $date = date ("F d Y H:i:s.", filemtime($fileInfo));

            $data[$key] = [$path, $olders, $data];
        }


// output each row of the data
    foreach ($data as $columns)
    {
        fputcsv($file, $columns);
    }

    fclose($file);
    exit();
2

The problem should be solvable by changing how you load the values in your first code-snippet. If you want the structure to be something like

[0] => Array
        (
            ['fpath'] => https://www.example.com/GHI/123/new.php
            ['folder'] => GHI/123
            ['date'] => April 12 2018 11:15:03.
        )
[1] => Array
        (
            ['fpath'] => https://www.example.com/GHI/123/new.php
            ['folder'] => GHI/123
            ['date'] => April 12 2018 11:15:03.
        )

Then the following should work for you:

<?php
// output headers so that the file is downloaded rather than displayed
    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename="csv.csv"');

// do not cache the file
    header('Pragma: no-cache');
    header('Expires: 0');

// create a file pointer connected to the output stream
    $file = fopen('php://output', 'w');

// send the column headers
    fputcsv($file, array('Filepath', 'Folders', 'Date'));

// data
    $scanner = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
    $data = array();

    foreach ($scanner as $fileInfo) {
        $data[] = array(
                    "fpath" => "https://www.example.com/" . substr($fileInfo->getPathname(), 2),
                    "folder" => substr(pathinfo($fileInfo->getPathname(), PATHINFO_DIRNAME), 2),
                    "date" => date ("F d Y H:i:s.", filemtime($fileInfo))
                );
    }

// output each row of the data
    foreach ($data as $column)
    {
        fputcsv($file, $column);
    }

    exit();
mtr.web
  • 1,505
  • 1
  • 13
  • 19
  • @mickmackusa - any better? I do not consider myself a master of PHP, but the edit at least covered most of your suggestions, and it would appear that it solved the problem, considering the OP accepted it. – mtr.web Apr 13 '18 at 13:44