<?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 完成它,但它不起作用。也许有人有一个建议。谢谢!