Reputation: 546
I'm trying to name my uploaded image based on the date & time it is being uploaded. So i used the following code to store the image name:
$imageName = date('d-m-y -H-i-s').'.'.$request->image->getClientOriginalExtension();
And it works pretty fine and my image name is like "6-6-2018 -14-14-14". But i want to achieve something like this "6-6-2018 14:14:14 ". So i used the below code but it shows error.
$imageName = date('d-m-y H:i:s').'.'.$request->image->getClientOriginalExtension();
The error is :
Could not move the file "C:\xampp\tmp\php1CBE.tmp" to "C:\xampp\htdocs\test\public/storage/images\16-08-18 18:39:08.jpg" (move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1CBE.tmp' to 'C:\xampp\htdocs\test\public/storage/images\16-08-18 18:39:08.jpg')
How to solve this? Please help me .
Upvotes: 0
Views: 55
Reputation: 717
You cannot use a colon in a filename but here's a nice alternative
$imageName = date('YmdHis').'.'.$request->image->getClientOriginalExtension();
For example the filename is:
20180816170102.jpg
Where you display the image, you can convert this string to any date format
echo date("d-m-y H:i:s", strtotime(substr($filename, 0, 14));
16-08-18 17:01:01
Another plus is that the files are now also sorted correctly in your filesystem.
20180816170102.jpg
20180813133156.jpg
20180718190809.jpg
Upvotes: 1