|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
File System commands in php
hello i had a question.
i have made a function which opens an existing text file in write and append mode. puts the data in the file and then closes the file. i call this function in a loop and then add each row from my database to this text file. what i want is that the next time i run the php script the contents in the file should become empty. is there any way to do it. i am pasting my function which i have written. thanks function appendToFile ($filename, $data) { //Open file $file = fopen($filename,"aw"); //Write data fwrite($file,$data); //Close file fclose($file); } |
|
#2
|
|||
|
|||
|
RE: File System commands in php
At the top of the script, just use the unlink command. That will delete the file. Then when your function is called, it will recreate it. Each time the script it called, it will delete the file and so forth...
|
|
#3
|
|||
|
|||
|
RE: File System commands in php
It sounds like you want a temp file... Use tmpfile() and get rid of your function. If you're calling in a while loop, there's no reason for it to be a function. Plus, every time you call it, you fopen() and fclose() which is unnecessary.
Just create the temp file before the loop, write to it in the loop, and fclose() is after the loop. |
|
#4
|
|||
|
|||
|
RE: File System commands in php
if i create tmpfile...will it get deleted after my script runs???
also if i use unlink....the file will get deleted...i like that idea...how do i create the file everytime them my script runs.. for example if the file name is test.txt so do i do fopen("/home/abc/test.txt", $data) |
|
#5
|
|||
|
|||
|
RE: File System commands in php
fopen("/home/abc/test.txt", 'w+');
|
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > File System commands in php |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|