...

View Full Version : Basic write file problem



disto
08-24-2006, 02:42 PM
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


<?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?

Anthony2oo4
08-24-2006, 03:42 PM
you could have a link such as

index.php?action=clearlog

then in your index put


<?
$action = $_GET['action'];

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

ronverdonk
08-24-2006, 03:59 PM
Assume your piece of code is named empty_file.php and is as follows:

<?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
$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 :cool:

disto
08-24-2006, 05:04 PM
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?

ronverdonk
08-24-2006, 05:20 PM
Since I hardly ever used Perl, I couldn't advise you.

Ronald :cool:

mlseim
08-24-2006, 05:36 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum