Appending Date/Time Stamp to filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Don

    #1

    Appending Date/Time Stamp to filename

    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.getMinute s().toString();
    sToday += today.getSecond s().toString();
    return sToday;
    }
    </script>
    <form enctype="multip art/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_f ile($_FILES['userfile']['tmp_name'][1],
    $uploadfile2);

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

    if ($_FILES['userfile']['size'][3] != 0)
    move_uploaded_f ile($_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! =-----
  • Lee

    #2
    Re: Appending Date/Time Stamp to filename

    Don said:[color=blue]
    >
    >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.[/color]
    [color=blue]
    >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.getMinute s().toString();
    > sToday += today.getSecond s().toString();
    > return sToday;
    >}
    ></script>[/color]

    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.

    Comment

    • Don

      #3
      Re: Appending Date/Time Stamp to filename

      On 26 Sep 2004 09:20:16 -0700, Lee <REM0VElbspamtr ap@cox.net> wrote:
      [color=blue]
      >Don said:[color=green]
      >>
      >>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.[/color]
      >[color=green]
      >>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.getMinute s().toString();
      >> sToday += today.getSecond s().toString();
      >> return sToday;
      >>}
      >></script>[/color]
      >
      >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.[/color]

      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="multip art/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_f ile($_FILES['userfile']['tmp_name'][0],
      $uploadfile1);

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

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

      if ($_FILES['userfile']['size'][3] != 0)
      move_uploaded_f ile($_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! =-----

      Comment

      • Randy Webb

        #4
        Re: Appending Date/Time Stamp to filename

        Don wrote:

        [color=blue]
        > Hi Lee,
        >
        > I'm not done with function datetimestamp. I intend to do as you
        > suggested.[/color]

        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.
        [color=blue]
        > 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.[/color]

        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.javas cript FAQ - http://jibbering.com/faq

        Comment

        • Don

          #5
          Re: Appending Date/Time Stamp to filename

          On Sun, 26 Sep 2004 15:21:41 -0400, Randy Webb
          <HikksNotAtHome @aol.com> wrote:
          [color=blue]
          >Don wrote:
          >
          >[color=green]
          >> Hi Lee,
          >>
          >> I'm not done with function datetimestamp. I intend to do as you
          >> suggested.[/color]
          >
          >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.
          >[color=green]
          >> 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.[/color]
          >
          >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-->[/color]
          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! =-----

          Comment

          • Randy Webb

            #6
            Re: Appending Date/Time Stamp to filename

            Don wrote:

            <--snip-->
            [color=blue]
            >
            > 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.[/color]

            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.javas cript FAQ - http://jibbering.com/faq

            Comment

            • Dr John Stockton

              #7
              Appending Date/Time Stamp to filename

              JRS: In article <7kidl0pjoa0m7q hufi0l5r300j0fg 0tg9k@4ax.com>, dated
              Sun, 26 Sep 2004 07:04:22, seen in news:comp.lang. javascript, Don
              <no@adr.com> posted :
              [color=blue]
              >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.getMinute s().toString();
              > sToday += today.getSecond s().toString();
              > return sToday;
              >}[/color]

              The concatenation is more effectively done as

              var sToday = "" +
              today.getMonth( )+1) +
              today.getDate() +
              today.getYear() +
              today.getHours( ) +
              today.getMinute s() +
              today.getSecond s() ;

              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())*1 00 + 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.demo n.co.uk/> - w. FAQish topics, links, acronyms
              PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
              Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

              Comment

              • Don

                #8
                Re: Appending Date/Time Stamp to filename

                On Sun, 26 Sep 2004 22:06:10 +0100, Dr John Stockton
                <spam@merlyn.de mon.co.uk> wrote:
                [color=blue]
                >JRS: In article <7kidl0pjoa0m7q hufi0l5r300j0fg 0tg9k@4ax.com>, dated
                >Sun, 26 Sep 2004 07:04:22, seen in news:comp.lang. javascript, Don
                ><no@adr.com> posted :
                >[color=green]
                >>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.getMinute s().toString();
                >> sToday += today.getSecond s().toString();
                >> return sToday;
                >>}[/color]
                >
                >The concatenation is more effectively done as
                >
                > var sToday = "" +
                > today.getMonth( )+1) +
                > today.getDate() +
                > today.getYear() +
                > today.getHours( ) +
                > today.getMinute s() +
                > today.getSecond s() ;
                >
                >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())*1 00 + getSeconds()
                >
                >For leading zeroes AND separators, use <FAQENTRY>
                >
                > function LZ(x) { return (x<0||x>=10?"": "0") + x }
                >
                >
                > *** DO NOT MULTI-POST ***
                >
                >[/color]
                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! =-----

                Comment

                Working...