Clearing content of text file using php

2020-02-16 07:37发布

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?

7条回答
叛逆
2楼-- · 2020-02-16 08:06

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.

查看更多
登录 后发表回答