Add time to file upload name
Discuss coding issues, and scripts related to PHP and MySQL.
-
melmdoost
- Posts:20
Add time to file upload name
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.
Admin
Posts:805
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
melmdoost
Posts:20
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!
Admin
Posts:805
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);
Similar Topics
- Change the max upload size in XAMPP
PHP - MySQL
First post
Pleassant Marplo, hope all things are okey ?
I have a question, I think I have server problem with uploading maximal upload-file (mb) is to less I...
Last post
Marplo in once thanks for the great suport
- GET_id not working in UnLink (delete file)
PHP - MySQL
First post
I searching for an hour for a solution; unlink seems not to work with GET id
<?php
$id = (int) $_GET ;
echo $_GET ;
$file = fopen( '.$_GET...
Last post
Here is an answer `o god after 2 hours shame on me for this one`
<?php
$file_pointer = $_GET .'.txt';
if (!unlink($file_pointer)) {
echo (...