简体   繁体   English

如何使用php和javascript擦除文本文件的内容

[英]How to erase the contents of a text file using php & javascript

I have a webpage that displays the contents of a text file. 我有一个网页,显示文本文件的内容。 I want to make a button that will clear the contents of that text file. 我要创建一个按钮,以清除该文本文件的内容。 I know that the contents of a file can be erased by using php, but I've been having trouble finding a way to make a button that will do this because php is server side. 我知道可以使用php擦除文件的内容,但是由于php是服务器端,因此我一直难以找到一种方法来制作将要执行此操作的按钮。 Is there a good way to do this with Javascript? 是否有使用Javascript的好方法?

A file, on the server, can only be deleted by software running on the server. 服务器上的文件只能由服务器上运行的软件删除。 Client side JavaScript can't do that. 客户端JavaScript无法做到这一点。 If it could, then any old Joe could delete any content from any website. 如果可以,那么任何老乔都可以从任何网站删除任何内容。

You could use client side JavaScript to make an HTTP request that the server (usually via a server side programming language (which could be JavaScript (via Node, Classic ASP, etc), PHP or something else)) would interpret as an instruction to delete the file … but you could equally just use an HTML form to make a POST request. 您可以使用客户端JavaScript发出HTTP请求 ,该请求(通常是通过服务器端编程语言(可以是JavaScript(通过Node,Classic ASP等),PHP或其他方式)通过服务器)解释为删除指令。文件...但是您可以同样地使用HTML表单发出POST请求。

No, you cannot modify the user's disk from within javascript. 不可以,您无法在javascript中修改用户的磁盘。 However, flash or silverlight might be able to do it. 但是,闪光灯或Silverlight可以做到这一点。 I shudder to think of the possibilities. 我不禁思索各种可能性。

edit : Oh, you're talking about a file on the server? 编辑 :哦,您正在谈论服务器上的文件? The server can do that. 服务器可以做到这一点。 The client can only instruct the server to do it on their behalf. 客户端只能指示服务器代表他们执行此操作。

You no need the Javascript. 您不需要Java语言。 Try something like this: 尝试这样的事情:

<form action="file.php" method="get">
   <button name="button_name" value="clear" />
</form>

And into your php file you need put something like this: 并在您的php文件中,您需要输入以下内容:

file_put_contents($file, ''); //write empty string to file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM