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

上記のコードで、3 つの列を持つ 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.
        )

)

したがって、現在の問題は、すべてのファイルパスが最初の配列/行に表示され、フォルダー名が 2 番目に、日付が最後に表示されることです。配列を csv で正しく表示するには、配列 / 行ごとに 3 つの値 (ファイルパス、フォルダー、日付) が必要です。

array_flip で実行しようとしましたが、うまくいきません。多分誰かがアドバイスを持っています。ありがとう!

4

2 に答える 2

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 に答える