Clearing content of text file using php

PhpHtmlText Files

Php Problem Overview


I have a filelist.txt file and I created a file called clear.php to clear the content of filelist.

I put a button in index.html to call clear.php to clear the file.

Can anyone help me out regarding what PHP code I should write in clear.php?

How to code a button to call clear.php and then return back to index.html showing the result that it has been cleared?

Php Solutions


Solution 1 - Php

http://php.net/manual/en/function.file-put-contents.php">file_put_contents</a>("filelist.txt";, "");

You can redirect by using the header() function to modify the Location header.

Solution 2 - Php

This would truncate the file:

$fh = fopen( 'filelist.txt', 'w' );
fclose($fh);

In clear.php, redirect to the caller page by making use of $_SERVER['HTTP_REFERER'] value.

Solution 3 - Php

//create a file handler by opening the file
$myTextFileHandler = @fopen("filelist.txt","r+");

//truncate the file to zero
//or you could have used the write method and written nothing to it
@ftruncate($myTextFileHandler, 0);

//use location header to go back to index.html
header("Location:index.html");

I don't exactly know where u want to show the result.

Solution 4 - Php

To add button you may use either jQuery libraries or simple Javascript script as shown below:

HTML link or button:

<a href="#" onClick="goclear()" id="button">click event</a>

Javascript:

<script type="text/javascript">
var btn = document.getElementById('button');
function goclear() { 
alert("Handler called. Page will redirect to clear.php");
document.location.href = "clear.php";
};
</script>

Use PHP to clear a file content. For instance you can use the fseek($fp, 0); or ftruncate ( resource $file , int $size ) as below:

<?php
//open file to write
$fp = fopen("/tmp/file.txt", "r+");
// clear content to 0 bits
ftruncate($fp, 0);
//close file
fclose($fp);
?>

Redirect PHP - you can use header ( string $string [, bool $replace = true [, int $http_response_code ]] )

<?php
header('Location: getbacktoindex.html');
?>

I hope it's help.

Solution 5 - Php

Use 'w' and not, 'a'.

if (!$handle = fopen($file, 'w'))

Solution 6 - Php

Try fopen() http://www.php.net/manual/en/function.fopen.php

w as mode will truncate the file.

Solution 7 - Php

 $fp = fopen("$address",'w+');
 if(!$fp)
    echo 'not Open';
        //-----------------------------------
 while(!feof($fp))
     {
        fputs($fp,' ',999);                    
     } 
 
 fclose($fp);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSubho HalderView Question on Stackoverflow
Solution 1 - PhpAndy EView Answer on Stackoverflow
Solution 2 - PhpAlan Haggai AlaviView Answer on Stackoverflow
Solution 3 - PhpTyrView Answer on Stackoverflow
Solution 4 - PhpDariusz JView Answer on Stackoverflow
Solution 5 - Phpuser268740View Answer on Stackoverflow
Solution 6 - PhpFredrikView Answer on Stackoverflow
Solution 7 - PhpJavad MasoumiView Answer on Stackoverflow