将创建的文件放入日期文件夹

时间:2019-05-06 13:54:42

标签: php date mkdir file-put-contents

我做了一些文件名。

首先,我需要创建带有年份和月份的文件夹,然后在该文件夹中放置新创建的csv文件。

除了我需要将该csv文件放入新创建的文件夹中的那一部分之外,所有内容似乎都可以正常工作。

创建文件并创建文件夹。

有人可以帮助解决这个问题。

它放在文件夹外面。

我的代码:

    // get directory path to save csv files
    $rootDir = $this->container->get('kernel')->getRootDir();
    $dir = $rootDir . '/../web/uploads/files/';

    // makeing new directory by date
    if(!is_dir($dir . date('Y-m'))) {
        mkdir($dir . date('Y-m'), 0777, true);
    }

    // generating csv file name
    $fileName = 'export-'.date('Y-m-d').'.csv';
    $fp = fopen($dir .$fileName, 'w');

1 个答案:

答案 0 :(得分:0)

您创建了包含年份和月份的文件夹,但从未将新文件夹添加到$dir变量中。

尝试一下:

$rootDir = $this->container->get('kernel')->getRootDir();

// Let's add the full destination here (including the month-dir)
$dir = $rootDir . '/../web/uploads/files/' . date('Y-m');

// Now we don't need to append the date since it's already included
if(!is_dir($dir)) {
    mkdir($dir, 0777, true);
}

// generating csv file name
$fileName = 'export-'.date('Y-m-d').'.csv';

// Just add a / and the filename and it should be the correct path
$fp = fopen($dir . '/' . $fileName, 'w');