PHP disable OR overwrite own / current file by itself

Task: cut or delete the file after the first pass.

I have an installation file called "index.php" that creates another php file.

<? /* here some code*/ $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?php \n echo 'hallo, *very very long text*'; \n ?>"; fwrite($fh, $stringData); /*herecut"/ /*here some code */ 

after creating a new file, this file is called, and I intend to erase the filecreation, since it is very long and is needed only during the first installation.

i to add to the above code

 echo 'hallo, *very very long text*'; \n ***$new= file_get_contents('index.php'); \n $findme = 'habanot'; $pos = strpos($new, $findme); if ($pos === false) { $marker='herecut';\n $new=strstr($new,$marker);\n $new='<?php \n /*habanot*/\n'.$new;\n $fh = fopen('index.php', 'w') or die 'cant open file'); $stringData = $new; fwrite($fh, $stringData); fclose($fh);*** ?>"; fwrite($fh, $stringData);]} 

Is there an easier way or function to change the current file or even β€œdestroy” the file after the first call?

Hi

EDIT: found a way to edit, sorry zaf

 unlink(__FILE__); 

can be used to delete the "auxiliary file" after execution.

+4
source share
2 answers
 unlink(__FILE__); 

for the file "helper" seems necessary, since I can not find a way to change the php file inuse / process.

+5
source

Most self-tuning PHP sites use install.php to do the initial setup. When the installation is verified, you must redirect to removeinstall.php, which will call unlink () for each installation file to clear everything.

This leaves removeinstall.php behind, but it makes sense not to pollute any of the "live code" with the installation code.

removeinstall.php will just contain unlink instructions ...

 if (file_exists('install.php')) { unlink('install.php'); } 

If you do not want to leave the removeinstall.php file, you may have a conditional call in another file ... for example index.php? removeinstallation = 1 or similar.

+1
source

Source: https://habr.com/ru/post/1309195/


All Articles