Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Appending Date/Time to File Upload

Posted on 2014-01-07
5
Medium Priority
?
1,169 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
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 12

Author Comment

by:Nathan Riley
ID: 39764066
Ok thanks guys, I'll go with either the . or _.
0
 
LVL 111

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 12

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

971 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