Solved

Appending Date/Time to File Upload

Posted on 2014-01-07
5
1,133 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:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

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 110

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:Nathan Riley
ID: 39764066
Ok thanks guys, I'll go with either the . or _.
0
 
LVL 110

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:Nathan Riley
ID: 39765115
Ah gotcha, will stick with underscore then.  Thanks!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

631 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