[ACCEPTED]-Append date to filename before copy in php-copy

Accepted answer
Score: 14

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

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

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

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

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

Score: 2

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

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

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

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

0

Score: 1

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

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

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

0

More Related questions