Page 1 of 1

Add time to file upload name

Posted: 13 Nov 2016, 17:47
by melmdoost
Hello.
i have a script for upload file. this script has a problem with duplicate names, in upload folder. i want rename file name in upload folder. i want add date and time after file name when uploaded. how can i do it?
for example file name is 'abc.jpg'. i want rename file name after uploaded in upload folder to: abc' + '$time'.jpg.

Code: Select all

$time = date("H:i:s", time()+$timezone);

old name=abc.jpg
new name= abc $time.jpg
- php script:

Code: Select all

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

thank you.

Add time to file upload name

Posted: 13 Nov 2016, 18:45
by Admin
Hello,
See if this script is useful and try addapt it in your code.

Code: Select all

$time = date('H-i-s', time());
$name ='abc.jpg';
$extension ='jpg';
$name = preg_replace('/(.+?)\.'. $extension .'$/i', '$1_'. $time .'.'. $extension, $name);
echo $name;  // abc_19-39-11.jpg

Add time to file upload name

Posted: 16 Nov 2016, 16:17
by melmdoost
Hello.
the above mentioned script works great, but it does not work with this:

Code: Select all

$timezone = 0;
$now = date("Y-m-d", time()+$timezone);
$time = date("H:i:s", time()+$timezone);
$time2=$now.'-'.$time;
$name = preg_replace('/(.+?)\.'. $extension .'$/i', '$1_'. $time2 .'.'. $extension, $name);
and it show this message:

Code: Select all

Warning: move_uploaded_file(upload/abc_2016-11-16-17:04:29.jpg): failed to open stream: Invalid argument in C:\xampp\htdocs\upload\upload_file.php on line 47
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpA075.tmp' to 'upload/abc_2016-11-16-17:04:29.jpg' in C:\xampp\htdocs\upload\upload_file.php on line 47
and it can not move file to upload folder!

Add time to file upload name

Posted: 16 Nov 2016, 18:27
by Admin
The character ":" is not allowed in file name; so, you should replace it with "-" or "_" in the string with the time.

Code: Select all

$time = date("H-i-s", time()+$timezone);