Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Basic write file problem

    i have a couple of counters and text log files on my site and i want to have a reset/clear log button. this script should clear the text file

    Code:
    <?php
    print "File cleared";
    $fp = fopen( "test.txt", "w" );
    fwrite( $fp, "" );
    fclose( $fp );
    print "End";
    ?>
    how would use a function so that when a text link/button was pressed the code above was run erasing the text file?

  • #2
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    you could have a link such as

    index.php?action=clearlog

    then in your index put

    PHP Code:
    <?
    $action 
    $_GET['action'];

    function 
    ClearLog() {
    print 
    "File cleared";
    $fp fopen"test.txt""w" );
    fwrite$fp"" );
    fclose$fp );
    print 
    "End";
    }
    if (
    $action == "clearlog") {
    ClearLog();
    }
    ?>

  • #3
    New Coder
    Join Date
    Aug 2006
    Location
    Netherlands
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Assume your piece of code is named empty_file.php and is as follows:
    PHP Code:
    <?php
    $fn 
    $_GET['name'];
    print 
    "File cleared";
    $fp fopen$fn"w" );
    fwrite$fp"" );
    fclose$fp );
    print 
    "End";
    ?>
    The following would present a list of emptyable files from an array.
    PHP Code:
    <?php
    $files 
    = array("file1.txt""file2.txt""file3.hlp");
    echo 
    "Click Empty to erase the file's contents<br />";
    echo 
    "<table border='0'>";
    echo 
    "<tr><th>Action</th><th>File name</th></tr>";
    for (
    $i=0$i count($files); $i++)
      echo 
    "<tr><td><a href='empty_file?name=$files[$i]'>Empty</a></td><td>$files[$i]</td></tr>";

    echo 
    "</table>";
    ?>
    Ronald
    RTFM is an almost extinct art form, it should be subsidized.

  • #4
    New Coder
    Join Date
    Sep 2005
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    great thanks alot forgot i could use the url and functions, im more familiar with cgi but thats opened quite alot in php.
    now im considering using php instead of perl for my counters system, what do you think any great advantage for doing that?

  • #5
    New Coder
    Join Date
    Aug 2006
    Location
    Netherlands
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since I hardly ever used Perl, I couldn't advise you.

    Ronald
    RTFM is an almost extinct art form, it should be subsidized.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Perl is much more powerful with string manipulation and processing.
    That, and speed, is its biggest asset.

    PHP can be easier just because you can upload the scripts in the same
    directory as your regular HTML files ... no cgi-bin required.

    But functionality, both are equally OK to use.


  •  

    Posting Permissions

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