By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,676 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,676 IT Pros & Developers. It's quick & easy.

Appending Date/Time Stamp to filename

P: n/a
Don
Hi all,

With regards to the following, how do I append the datetimestamp to
the filenames in the form? The files are processed using the PHP
script that follows below.

Thanks in advance,
Don

Following running on client-side:
--------------------------------------

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
function datetimestamp()
{
var today = new Date();
var sToday = (today.getMonth()+1).toString();
sToday += today.getDate().toString();
sToday += today.getYear().toString();
sToday += today.getHours().toString();
sToday += today.getMinutes().toString();
sToday += today.getSeconds().toString();
return sToday;
}
</script>
<form enctype="multipart/form-data" action="http://notRealURL.php"
method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
<input type="hidden" name="MAX_INPUT_TIME" value="280" />
Send file 1: <input name="userfile[]" type="file" /><br />
Send file 2: <input name="userfile[]" type="file" /><br />
Send file 3: <input name="userfile[]" type="file" /><br />
Send file 4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Send File" />
</form>
</body>

Following server-side PHP script receives and uploads the files:
---------------------------------------------------------------------------

<?php
$uploaddir = 'picturevault/';
$uploadfile1 = $uploaddir . $_FILES['userfile']['name'][0];
$uploadfile2 = $uploaddir . $_FILES['userfile']['name'][1];
$uploadfile3 = $uploaddir . $_FILES['userfile']['name'][2];
$uploadfile4 = $uploaddir . $_FILES['userfile']['name'][3];

print "<pre>";

if ($_FILES['userfile']['size'][0] != 0) {
if (move_uploaded_file($_FILES['userfile']['tmp_name'][0],
$uploadfile1)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging
info:\n";
print_r($_FILES);
}
print "</pre>";
}

if ($_FILES['userfile']['size'][1] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][1],
$uploadfile2);

if ($_FILES['userfile']['size'][2] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][2],
$uploadfile3);

if ($_FILES['userfile']['size'][3] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][3],
$uploadfile4);

print "</pre>";

?>


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lee
Don said:

Hi all,

With regards to the following, how do I append the datetimestamp to
the filenames in the form? The files are processed using the PHP
script that follows below. function datetimestamp()
{
var today = new Date();
var sToday = (today.getMonth()+1).toString();
sToday += today.getDate().toString();
sToday += today.getYear().toString();
sToday += today.getHours().toString();
sToday += today.getMinutes().toString();
sToday += today.getSeconds().toString();
return sToday;
}
</script>


That's a very bad format for a date stamp. Ignoring the
similar problems with hours minutes and seconds, how would
you interpret the date stamp: "1122004"? Is that November
2nd, or is it January 12th? You need to pad any single-digit
values to two digits. Then, you'll probably want to change
the order to Year,Month,Date,Hours,Minutes,Seconds, so you
can sort the values easily.

If you want to pass the client's clock time to the server,
there's no need to append it to the filename on the client
side (not possible, anyway). Just pass it in a hidden form
field. However, it would be simpler and more reliable to
have your PHP script determine the time on the server. You
never know whether the client's clock is correct, and you're
not recording their timezone.

Jul 23 '05 #2

P: n/a
Don
On 26 Sep 2004 09:20:16 -0700, Lee <RE**************@cox.net> wrote:
Don said:

Hi all,

With regards to the following, how do I append the datetimestamp to
the filenames in the form? The files are processed using the PHP
script that follows below.

function datetimestamp()
{
var today = new Date();
var sToday = (today.getMonth()+1).toString();
sToday += today.getDate().toString();
sToday += today.getYear().toString();
sToday += today.getHours().toString();
sToday += today.getMinutes().toString();
sToday += today.getSeconds().toString();
return sToday;
}
</script>


That's a very bad format for a date stamp. Ignoring the
similar problems with hours minutes and seconds, how would
you interpret the date stamp: "1122004"? Is that November
2nd, or is it January 12th? You need to pad any single-digit
values to two digits. Then, you'll probably want to change
the order to Year,Month,Date,Hours,Minutes,Seconds, so you
can sort the values easily.

If you want to pass the client's clock time to the server,
there's no need to append it to the filename on the client
side (not possible, anyway). Just pass it in a hidden form
field. However, it would be simpler and more reliable to
have your PHP script determine the time on the server. You
never know whether the client's clock is correct, and you're
not recording their timezone.


Hi Lee,

I'm not done with function datetimestamp. I intend to do as you
suggested.

Regarding passing the datetimestamp to the PHP script on the
server-side, can you show me some code on the client-side (html),and
in the PHP script on the server-side? I'm quite new at JS
programming, and am not sure how to go about it. I will still need to
have the server-side PHP script append that datetimestamp to the file
name. The purpose of these scripts is to allow uploading of 1 - 4
picture fiiles from the client's desktop. The reason for appending
the datetime to the file name is to ensure another client can't easily
overwrite a previous client's upload.

Here's the client-side html:
--------------------------------
<form enctype="multipart/form-data"
action="http://notRealURL.com/cpcenhanced/processpics.php"
method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
<input type="hidden" name="MAX_INPUT_TIME" value="280" />
Send file 1: <input name="userfile[]" type="file" /><br />
Send file 2: <input name="userfile[]" type="file" /><br />
Send file 3: <input name="userfile[]" type="file" /><br />
Send file 4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Send File" />
</form>

And, here's the server-side PHP:
---------------------------------------
<?php
$uploaddir = 'picturevault/';
$uploadfile1 = $uploaddir . $_FILES['userfile']['name'][0];
$uploadfile2 = $uploaddir . $_FILES['userfile']['name'][1];
$uploadfile3 = $uploaddir . $_FILES['userfile']['name'][2];
$uploadfile4 = $uploaddir . $_FILES['userfile']['name'][3];

print "<pre>";

if ($_FILES['userfile']['size'][0] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][0],
$uploadfile1);

if ($_FILES['userfile']['size'][1] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][1],
$uploadfile2);

if ($_FILES['userfile']['size'][2] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][2],
$uploadfile3);

if ($_FILES['userfile']['size'][3] != 0)
move_uploaded_file($_FILES['userfile']['tmp_name'][3],
$uploadfile4);

print "</pre>";

?>

Thank you
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #3

P: n/a
Don wrote:

Hi Lee,

I'm not done with function datetimestamp. I intend to do as you
suggested.
If you do it on the server, then you have no need to attempt to do it on
the client. As Lee said, you can't do what you are wanting anyway. And
even if you could modify the value of a file input field, it's still not
the best way to do it.
Regarding passing the datetimestamp to the PHP script on the
server-side, can you show me some code on the client-side (html),and
in the PHP script on the server-side? I'm quite new at JS
programming, and am not sure how to go about it. I will still need to
have the server-side PHP script append that datetimestamp to the file
name. The purpose of these scripts is to allow uploading of 1 - 4
picture fiiles from the client's desktop. The reason for appending
the datetime to the file name is to ensure another client can't easily
overwrite a previous client's upload.


That is why you need to append the datetime on the server, not on the
client. Let's say Lee and I both attach files, and the time on *our*
computers generate the exact same date/time stamp. Then, you have the
problem. It also does not prevent my clock from being wrong and me
resetting it and overwriting my own files. By doing the date time stamp
on the server, you have a lot more control over the time stamp.

A better, more reliable, method would be to assign them numbers in
numerical order. Check the last file name, increase its counter, and
carry on.

<--snip-->
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4

P: n/a
Don
On Sun, 26 Sep 2004 15:21:41 -0400, Randy Webb
<Hi************@aol.com> wrote:
Don wrote:

Hi Lee,

I'm not done with function datetimestamp. I intend to do as you
suggested.


If you do it on the server, then you have no need to attempt to do it on
the client. As Lee said, you can't do what you are wanting anyway. And
even if you could modify the value of a file input field, it's still not
the best way to do it.
Regarding passing the datetimestamp to the PHP script on the
server-side, can you show me some code on the client-side (html),and
in the PHP script on the server-side? I'm quite new at JS
programming, and am not sure how to go about it. I will still need to
have the server-side PHP script append that datetimestamp to the file
name. The purpose of these scripts is to allow uploading of 1 - 4
picture fiiles from the client's desktop. The reason for appending
the datetime to the file name is to ensure another client can't easily
overwrite a previous client's upload.


That is why you need to append the datetime on the server, not on the
client. Let's say Lee and I both attach files, and the time on *our*
computers generate the exact same date/time stamp. Then, you have the
problem. It also does not prevent my clock from being wrong and me
resetting it and overwriting my own files. By doing the date time stamp
on the server, you have a lot more control over the time stamp.

A better, more reliable, method would be to assign them numbers in
numerical order. Check the last file name, increase its counter, and
carry on.

<--snip-->

Hi Randy,

The reason I wanted to append the date/time stamp on the client-side
is that that same composite file name is then going to be used in the
same client-side page for other purposes. So, I need to know the
date/time stamp before I submit the form. However, I like even better
your idea of running a sequence counter on the server side. However,
again the client side page containing JS will need to know the
sequence number assigned by PHP on the server side. How do I
communicate back to the client page that submitted the form the
sequence number determined by the server side PHP? I'm finding this
quite difficult, but I sure am learning a lot.

Thanks for your help.
Don
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #5

P: n/a
Don wrote:

<--snip-->

Hi Randy,

The reason I wanted to append the date/time stamp on the client-side
is that that same composite file name is then going to be used in the
same client-side page for other purposes. So, I need to know the
date/time stamp before I submit the form. However, I like even better
your idea of running a sequence counter on the server side. However,
again the client side page containing JS will need to know the
sequence number assigned by PHP on the server side. How do I
communicate back to the client page that submitted the form the
sequence number determined by the server side PHP? I'm finding this
quite difficult, but I sure am learning a lot.


When the form gets submitted to the server, the page that the server
returns can have the image names in it. Or, post it to a hidden IFrame
and read them from there. For the non-JS browsing audience, the first
scenario is preferred.

Page1.php submits the images to Page2.php, Page2.php can get the names
of the images and include them in the page it returns. echo $imageName;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #6

P: n/a
JRS: In article <7k********************************@4ax.com>, dated
Sun, 26 Sep 2004 07:04:22, seen in news:comp.lang.javascript, Don
<no@adr.com> posted :
function datetimestamp()
{
var today = new Date();
var sToday = (today.getMonth()+1).toString();
sToday += today.getDate().toString();
sToday += today.getYear().toString();
sToday += today.getHours().toString();
sToday += today.getMinutes().toString();
sToday += today.getSeconds().toString();
return sToday;
}


The concatenation is more effectively done as

var sToday = "" +
today.getMonth()+1) +
today.getDate() +
today.getYear() +
today.getHours() +
today.getMinutes() +
today.getSeconds() ;

Unless you use field separators or leading zeroes or both, the result
will be horribly ambiguous if M<10 xor D<10, or any but not all of h m s
<10 (that assumes the year to be recognisable).

FFF is unwise for general use; follow FIPS 4.1 (if American) or ISO
8601.

The following gives a universally comprehensible result, and, after
thinking about it for the first time, will seem far simpler.

with (new Date()) sToday = ((((
getYear()*100 + (getMonth()+1))*100 + getDate())*100 +
getHours())*100 + getMinutes())*100 + getSeconds()

For leading zeroes AND separators, use <FAQENTRY>

function LZ(x) { return (x<0||x>=10?"":"0") + x }
*** DO NOT MULTI-POST ***
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #7

P: n/a
Don
On Sun, 26 Sep 2004 22:06:10 +0100, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <7k********************************@4ax.com>, dated
Sun, 26 Sep 2004 07:04:22, seen in news:comp.lang.javascript, Don
<no@adr.com> posted :
function datetimestamp()
{
var today = new Date();
var sToday = (today.getMonth()+1).toString();
sToday += today.getDate().toString();
sToday += today.getYear().toString();
sToday += today.getHours().toString();
sToday += today.getMinutes().toString();
sToday += today.getSeconds().toString();
return sToday;
}


The concatenation is more effectively done as

var sToday = "" +
today.getMonth()+1) +
today.getDate() +
today.getYear() +
today.getHours() +
today.getMinutes() +
today.getSeconds() ;

Unless you use field separators or leading zeroes or both, the result
will be horribly ambiguous if M<10 xor D<10, or any but not all of h m s
<10 (that assumes the year to be recognisable).

FFF is unwise for general use; follow FIPS 4.1 (if American) or ISO
8601.

The following gives a universally comprehensible result, and, after
thinking about it for the first time, will seem far simpler.

with (new Date()) sToday = ((((
getYear()*100 + (getMonth()+1))*100 + getDate())*100 +
getHours())*100 + getMinutes())*100 + getSeconds()

For leading zeroes AND separators, use <FAQENTRY>

function LZ(x) { return (x<0||x>=10?"":"0") + x }
*** DO NOT MULTI-POST ***

Thanks for your help John. Your function looks much better.
Regards,
Don
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.