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();

使用上面的代码,我试图获得一个包含三列的 csv。数组的输出如下所示:

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.
        )

)

所以现在的问题是所有文件路径都显示在第一个数组/行中,文件夹名称在第二个,日期在最后。为了让数组在 csv 中正确显示,我需要为每个数组/行提供三个值(文件路径、文件夹、日期)。

我试图用 array_flip 完成它,但它不起作用。也许有人有一个建议。谢谢!

4

2 回答 2

2

$data你可以在结构上尝试不同的方法

    $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();
于 2018-04-13T13:03:44.427 回答
2

该问题应该可以通过更改在第一个代码片段中加载值的方式来解决。如果您希望结构类似于

[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.
        )

那么以下内容应该适合您:

<?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();
于 2018-04-13T13:05:41.370 回答