PHP unlink OR rewrite own/current file by itself
Asked Answered
S

2

5

Task: Cut or erase a file after first walk-through.

i have an install file called "index.php" which 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 the creation of the new file this file is called and i intent to erase the filecreation call since it is very long and only needed on first install.

i therefor 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);]}

Isnt there an easier way or a function to modify the current file or even "self destroy" a file after first call?

Regards

EDIT: found the way to edit, sorry to zaf

unlink(__FILE__);

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

Santiago answered 8/5, 2010 at 16:48 Comment(0)
S
6
unlink(__FILE__);

for the "helper" file seems necessary since i cant find a way to modify the php-file inuse/process.

Santiago answered 8/5, 2010 at 18:30 Comment(2)
Dude(ss), thats not very sporting of you. Answering your own question using someone else's answer is usually frowned upon. I'm frowning anyway.Syllogistic
why, you did not mention unlink (FILE) (the underlines are not visible in comment area) and i only answered my question because i could not use code in the comment area. This is why i reposted that code for others with the correct syntax. Actually i dont know your problem but im sorry if i did something inappropriate.Santiago
E
1

Most self-installing PHP sites use an install.php to perform the initial set-up. When the install is verified, you would redirect to removeinstall.php which would call unlink() on each installation file to clear them all out.

This does leave behind the removeinstall.php, but has the benefit of not polluting any of the "live code" with installation removal code.

removeinstall.php would simply contain the unlink statements...

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

If you don't want to leave behind the removeinstall.php, you could have a conditional call in a different file... for example index.php?removeinstallation=1 or similar.

Epenthesis answered 11/5, 2010 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.