Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Save form data in a txt file with current date in file name

  1. #1
    Join Date
    Aug 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Save form data in a txt file with current date in file name

    I use the following script to save the data from a form to a txt file on the server with the name file_name.txt. What i would like is for the file name to be the date and time the file was posted in UTC eg. 2009-06-09-13-45-59.txt (year-month-day-hour-minute-second.txt)


    PHP Code:
    <?php


    // retrieve form data
    $input $_POST['msg'];



    // Open the file and erase the contents if any

    $fp fopen("file_name.txt""r+");



    // Write the data to the file

    fwrite($fp$input).'&nbsp;';



    // Close the file

    fclose($fp);



    ?>

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Do you only want 1 text file?

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you want a new file every time someone submits the form, correct? try this:
    PHP Code:
    <?php
    $input 
    $_POST['msg'];
    $dateposted date("YmdHis");
    $fp fopen("$dateposted.txt""w"); 
    fwrite($fp$input).'&nbsp;'
    fclose($fp); 
    ?>
    Notes:
    1) Tested and works.
    2) You would be well-advised to validate or sanitize the user input before writing it to a file, especially if you plan on using the contents later.
    3) You need to change the write mode to "w" instead of "r+".
    4) This names the file using 24-hr time (e.g., "20090607195136.txt"). if you want 12-hr time (e.g., "20090607075136.txt"), use this:
    PHP Code:
    $dateposted date("Ymdhis"); 
    though this could result in duplicate file names (thereby overwriting earlier submissions), IF someone happened to submit their form EXACTLY 12 hours after someone else did, unless you add AM or PM to the name (e.g., "20090607075136pm.txt"):
    PHP Code:
    $dateposted date("Ymdhisa"); 
    Edit:
    If you want the dashes, use: $dateposted = date("Y-m-d-H-i-s"); -I think I prefer it without, but it's your file.

    Read up here.
    Last edited by traq; 06-08-2009 at 03:18 AM.

  4. #4
    Join Date
    Aug 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Excellent work traq

    One thing "YmdHis" this does not write the time in 24hour format e.g. 6pm comes out as 0600 and not 1800 as it should be. Could this be a server issue as i see the time that the file is uploaded as 06:00.

    Also you mentioned validation and sanitzation it sounds very complicated, how could you scan the file for bad words and replace them with blanked out words.
    Last edited by trazix; 06-08-2009 at 05:31 PM.

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by trazix View Post
    One thing "YmdHis" this does not write the time in 24hour format... Could this be a server issue as i see the time that the file is uploaded as 06:00.
    odd. It did when I tested it on my server. That's what it's supposed to do.
    It could be a server issue. If you server is in a different time zone than you are (which is fairly likely), it could be 6AM there and not PM. Other than that, I don't know.

    Quote Originally Posted by trazix View Post
    Also you mentioned validation and sanitzation it sounds very complicated, how could you scan the file for bad words and replace them with blanked out words.
    By validation/ sanitization I didn't mean checking for bad words, etc., but checking that no harmful code is inadvertently saved to your website. For a start, read about htmlentities().

    You can find plenty of scripts that will search out and censor bad words, too. Just do a Google search.

  6. #6
    Join Date
    Aug 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I checked the location of the server and it is in Malysia, which is +8 hours from my time zone GMT.

    What if I wanted to use a number as the file name instead which increments by one every time a file is saved. What modification to the code would be needed.

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you could use a database or a text file to keep track of the last file name. A text file would be simpler. try this:
    PHP Code:
    <?php
    if (isset($_POST['msg'])){
    //process message if submitted
        
    if (file_exists("filecount.txt")){
        
    //find out if there is a file count file
            
    $filename file_get_contents("filecount.txt");
            
    //if so, use the stored number as the filename
        
    } else {
        
    //if not,
            
    $filename 1;
            
    //set the filename to "1"
            
    $fCount fopen("filecount.txt""w");
            
    //then create the file count file
            
    fwrite($fCount"2");
            
    //set its contents to "2", which will be the next filename
            
    fclose($fCount);
            
    //and close it
        
    }
        
    $input $_POST['msg'];
        if (!
    file_exists("$filename.txt")){
        
    //if the filename is not already in use,
            
    $fp fopen("$filename.txt""w");
            
    fwrite($fp$input).'&nbsp;'
            
    fclose($fp);
            
    //write the submitted message to a file using the file name
            
    $fInc fopen("filecount.txt""w");
            
    //then, open the file count file
            
    fwrite($fInc, (++$filename));
            
    //add one to the value and rewrite it (now it's ready for the next submission)
            
    fclose($fInc);
            
    //and close it
        
    } else {
        echo 
    "Couldn't save your message: File name already exists!";
        
    //otherwise, send an error message and DON'T OVERWRITE THE FILE!
        //some code could be developed here to choose a suitable filename, but I'm not sure how it would go
        
    }
    }
    ?>
    The first else{} block (the one that writes the initial "filecount.txt" file) is only intended to be used the first time a message is submitted. It could be left out if you prefer to create that file manually and upload it to the same directory.

    There's probably a way to read through the filenames and find the next one in line, but I'm not sure how to do it. But at that point, you wouldn't need the "filecount.txt" file in the first place.
    Last edited by traq; 06-15-2009 at 02:30 AM.

  8. #8
    Join Date
    Aug 2007
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Excellent work traq, you must know this php insideout, I am very much a novice at it. The file number is probably a better method.

    Now how could i get the content from these files and write them to a web page starting with the highest file number and placing it inside a table or Div for example.

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I'm glad I could help, but you are overestimating my ability. I've only been using php for a few months, and I'm still learning most of it. I try to make good use of tutorials, and I spend lots of time googling various error messages. If you're good with logic and troubleshooting, learning the basics of php shouldn't be too hard. That last bit (incrementing file naming) took me quite a bit of trial-and-error, and most of it is adapted from several php hit counter tutorials. Just try stuff and change one bit at a time until it all works.

    I have an idea of what would need to be done for what you're asking, but ATM I can only figure how to do it forwards. You need to find a function that will get all the filenames from a directory and loop through them in descending order... Give me a while and we'll see what we can figure out.

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Okay: here's what I have so far:
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    Comments:
    <textarea name="msg">I am layzeeee don't wanna type for each test</textarea>
    <input type="submit" value="submit">
    </form>
    <?php
    if (isset($_POST['msg'])){
    //process message if submitted
        
    if (file_exists("filecount.txt")){
        
    //find out if there is a file count file
            
    $filename file_get_contents("filecount.txt");
            
    //if so, use the stored number as the filename
        
    } else {
        
    //if not,
            
    $filename 1;
            
    //set the filename to "1"
            
    $fCount fopen("filecount.txt""w");
            
    //then create the file count file
            
    fwrite($fCount"2");
            
    //set its contents to "2", which will be the next filename
            
    fclose($fCount);
            
    //and close it
        
    }
        
    $input $_POST['msg'];
        if (!
    file_exists("write/$filename.txt")){
        
    //if the filename is not already in use,
            
    $fp fopen("write/$filename.txt""w");
            
    fwrite($fp$input).'&nbsp;'
            
    fclose($fp);
            
    //write the submitted message to a file using the file name
            
    $fInc fopen("filecount.txt""w");
            
    //then, open the file count file
            
    fwrite($fInc, (++$filename));
            
    //add one to the value and rewrite it (now it's ready for the next submission)
            
    fclose($fInc);
            
    //and close it
        
    }
        
    function 
    directory($result) {
        
    $h=opendir("write");
        while (
    $file readdir($h)) {
            if (
    $file == "." || $file == "..") { 
                
    //do nothing
            
    } else { 
                    
    $RAWfilecontent file_get_contents("write/$file");
                    
    $filecontent filter_var($RAWfilecontentFILTER_SANITIZE_STRING);
                    print 
    '
                        <div id="'
    .$file.'" class="entry">
                            '
    .$filecontent.'
                        </div>'
    ;
                }
            }
        
    closedir($h);
        return 
    $result;
    }
    echo 
    '<b>Comments submitted so far:</b>';
    echo 
    directory($result);
    }
    ?>
    This creates a filecount file, as before, and writes each submitted comment. This time, it saves those text files in another directory (write). This directory must contain only the comment submissions (1.txt, 2.txt, etc.).

    The function at the end reads the "write" directory and prints the contents of each comment file inside a <div> at the bottom of the page. FILTER_SANITZIE_STRING strips any code tags from the file contents to help prevent an attacker from running code through their comments.

    It works, however, there is no check on each file's type. The script assumes that all the files in the directory are supposed to be used. If a non-text file gets into the directory somehow, you could end up running code you didn't intend to.

    I've tried using various functions, including finfo() and mime_content_type, to verify the file type as plain text, but I can't get them to work properly yet.

    Working example here.
    Last edited by traq; 06-17-2009 at 06:35 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •