Solved

Appending Date/Time to File Upload

Posted on 2014-01-07
5
1,073 Views
Last Modified: 2014-01-08
How do I add a date and time to the end of my file upload?

Currently I've tried:
move_uploaded_file($_FILES["file"]["tmp_name"],
        "uploads/".$orgid."/".$_FILES["file"]["name"].date("Y-m-d H:i:s"));

Open in new window

This doesn't quite work as it appends like this: filename.jpg2014-01-06 12:01:05

How can I do this so that it will be filename2014-01-06 12:01:05.jpg
0
Comment
Question by:N R
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 39762336
1. You shouldn't use a colon in the filename. It's not compatible with every filesystem and can give you headaches later on. Also, if you can, avoid spaces, too. Use underscores and dashes to indicate those types of characters.

2. Without the above corrections, here's what you're likely looking for, but I would highly recommend using my suggestions in #1:

<?php
// Split the original filename by the period character
$pieces = explode(".",$_FILES["file"]["tmp_name"]);

// Grab the last piece, which should be "jpg"
$extension = array_pop($pieces);

// Put the remaining pieces back together with the dot character, add the date, and then add a dot and the extension to get the new filename.
$newfilename = implode(".",$pieces).date("Y-m-d H:i:s").".".$extension;

// Move the file
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$orgid."/".$newfilename);
?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39762439
Underscores work well in file names.  Ascii characters below code point 128 work well in file names, but for readability and common sense, you might want to limit the character set in the file names to alphanumeric + underscore.  Some file systems are not case-sensitive.

A date pattern that you might choose would look like this.  The leading underscore will make it more readable.  date('_Y_m_d_H_i_s');
0
 
LVL 11

Author Comment

by:N R
ID: 39764066
Ok thanks guys, I'll go with either the . or _.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39764072
Don't use the dot!  

Don't use the dot!

Instead use the underscore.  If you want to find out why I recommend against using the dot, try using the dot.  It takes too long to explain, and it will make a mess of your application.  But in life, "understanding" is the bobby prize!

Thanks for using EE, and thanks for the points, ~Ray
0
 
LVL 11

Author Comment

by:N R
ID: 39765115
Ah gotcha, will stick with underscore then.  Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now