Php – Adding timestamp and custom name to a File uploaded via PHP


I was looking for a way to add a timestamp and a custom name to a file uploaded to my server via php. I does some readings on $_FILE[] to understand its arguments, but the official PHP docs give no detailed explanation.

This is the code I use to upload, where could I include the timestamp (date('m-d-Y_H:i:s')) and name (ie: "myFile") to create a uniq name for files that are uploaded?


$date = date('m-d-Y_H:i:s');    
$file_path = "uploads/{$_POST['name']}/";

    if (!file_exists("uploads/{$_POST['name']}")) {
        mkdir("uploads/{$_POST['name']}", 0777, true);  
    } else {
        echo 'folder already exists!';

    $file_path = $file_path . basename($_FILES['zipFile']['name']);
    if (move_uploaded_file($_FILES['zipFile']['tmp_name'], $file_path)) {
        echo "success";
    } else {
        echo "fail";

Best Solution

Hope you can get an idea

$file_path = "uploads/".$_POST['name']."/";   
$newfile = $_POST['NAME'].date('m-d-Y_H:i:s')'.zip'; //Please use date('m-d-Y_H-i-s') for Linux. 
$filename = $file_path.$newfile;
        // Other codes
    echo 'file already exists';