Edmond Tamas Edmond Tamas - 2 months ago 17
PHP Question

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?

Thanks!

$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";
}

Answer

Hope you can get an idea

$file_path = "uploads/".$_POST['name']."/";   
$newfile = $_POST['NAME'].date('m-d-Y_H:i:s')'.zip';
$filename = $file_path.$newfile;
if(!file_exists($filename))
{
    if(move_uploaded_file($_FILES['zipFile']['tmp_name'],$filename))
    {
    // Other codes
    }
}
else
{
    echo 'file already exists';
}