php 使用php清除文本文件的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing content of text file using php
提问by Subho Halder
I have a filelist.txt file and I created a file called clear.php to clear the content of filelist.
我有一个 filelist.txt 文件,我创建了一个名为 clear.php 的文件来清除 filelist 的内容。
I put a button in index.html to call clear.php to clear the file.
我在 index.html 中放了一个按钮来调用 clear.php 来清除文件。
Can anyone help me out regarding what PHP code I should write in clear.php?
任何人都可以帮助我了解我应该在 clear.php 中编写哪些 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?
如何编写一个按钮来调用 clear.php 然后返回 index.html 显示它已被清除的结果?
回答by Andy E
file_put_contents("filelist.txt", "");
You can redirect by using the header()function to modify the Location header.
您可以通过使用header()函数来修改 Location 标头来重定向。
回答by Alan Haggai Alavi
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.
在 clear.php 中,使用$_SERVER['HTTP_REFERER']value重定向到调用者页面。
回答by Alan Haggai Alavi
//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.
我不完全知道你想在哪里显示结果。
回答by Dariusz J
To add buttonyou may use either jQuerylibraries or simple Javascript script as shown below:
要添加按钮,您可以使用jQuery库或简单的 Javascript 脚本,如下所示:
HTMLlink or button:
HTML链接或按钮:
<a href="#" onClick="goclear()" id="button">click event</a>
Javascript:
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 PHPto clear a file content. For instance you can use the fseek($fp, 0);or ftruncate ( resource $file , int $size )as below:
使用PHP清除文件内容。例如,您可以使用fseek($fp, 0); 或ftruncate ( resource $file , int $size )如下:
<?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 ( string $string [, bool $replace = true [, int $http_response_code ]] )
<?php
header('Location: getbacktoindex.html');
?>
I hope it's help.
我希望它有帮助。
回答by Dariusz J
Use 'w'and not, 'a'.
使用'w'而不是,'a'。
if (!$handle = fopen($file, 'w'))
回答by Fredrik
Try fopen() http://www.php.net/manual/en/function.fopen.php
试试 fopen() http://www.php.net/manual/en/function.fopen.php
w as mode will truncate the file.
w as 模式将截断文件。
回答by Javad Masoumi
$fp = fopen("$address",'w+');
if(!$fp)
echo 'not Open';
//-----------------------------------
while(!feof($fp))
{
fputs($fp,' ',999);
}
fclose($fp);

