Append date to filename before copy in php

落爺英雄遲暮 提交于 2019-12-05 19:22:46

Try it using a date format that doesn't include colons. Colons are not permitted in Windows filenames, and perhaps other filesystem types.

// Try, for example
$fileD = "file".date('m-d-Y-His A e').".csv";

As far as I see, your problem is because of identifier:

e Timezone identifier (added in PHP 5.1.0) Examples: UTC, GMT, Atlantic/Azores

But filename can't be with "/" inside. Try using another date format

You know, I'm always wary of putting spaces in file names, call it a throwback to the 80's and 90's. What happens if you just try:

var_dump(preg_replace('-\W-','_',date('m-d-Y H:i:s A e')));

You could do: $fileD = "file".$date.".csv";

Or: $fileD = "file{$date}.csv";

When you concatenate a statement (such as your date function) it should not be surrounded by quotes. So your second example should work written like so:

$fileD = "file".date('m-d-Y H:i:s A e').".csv";

Try this utility function:

public static function appendDateTimeToFileName($fileName) {
    $appended = date('_Y_m_d_H_i_s');
    $dotCount = substr_count($fileName, '.');
    if (!$dotCount) {
        return $fileName . $appended;
    }
    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
    $fileName = pathinfo($fileName, PATHINFO_FILENAME);
    return $fileName . $appended . '.' . $extension;
}

Example:

sample.jpg  -> sample_2017_02_19_01_09_10.jpg
sample      -> sample_2017_02_19_01_09_44
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!