Solved

Appending Date/Time to File Upload

Posted on 2014-01-07
5
1,046 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

18 Experts available now in Live!

Get 1:1 Help Now