If I understood your question correctly, you have a form and clicking of the "Rename" button should rename "log.html" to "OLD.LOG/log.html.bac.date" in the PHP server. I strongly discourage letting client side decide the name of the renamed file. With your current approach, the client side (browser) sends details of the renaming operation. This could be a security loop hole using which an attacker could rename an important file in the server.
Assuming that you are doing a simple form submit (no fancy ajax required), change the action to point to a php
file in the server.
<form method="post" action="/rename">
<input name="Rename" type="submit" id="Rename" value="Clear Page" >
<p class="logout"><a id="exit" href="#">Exit</a></p>
</form></p>
I assumed that you have setup clean URLs with some bootstrap/front controller to execute rename.php
in the PHP server otherwise, use
<form method="post" action="/rename.php">
then in rename.php
have code similar to the following
<?php
rename("log.html","OLD.LOG/log.html.bac" . time());
$fh = fopen("log.html","w");
fclose($fh);
?>
I added the time()
to prevent the back up file being overridden every single time. And always close the file handlers you open. I also assumed that you only want to create the empty log.html
, and not to read it. so w
is enough, w+
is not required.
Also, I assume that you have some authentication/authorization measures in place to prevent a DOS attack on the server by executing the rename operation too many.
If you want to use the same PHP file that outputs the form then pass an extra parameter in the POST request to the same URL. There are many ways you can do that, the simplest being that. But you can do it properly using proper HTTP headers, but having the same URL for displaying the form as well as for processing the form. There are many MVC frameworks like Zend framework which are capable of routing to different actions in the same controller by inspecting the HTTP method (GET, POST, DELETE, ect)