Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Appending Date/Time to File Upload

Posted on 2014-01-07
5
Medium Priority
?
1,147 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 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

718 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