PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 24th, 2002, 06:15 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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);
}

Reply With Quote
  #2  
Old September 24th, 2002, 06:21 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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...

Reply With Quote
  #3  
Old September 24th, 2002, 06:22 PM
guyer guyer is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Bloomington, IN USA
Posts: 47 guyer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to guyer
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.

Reply With Quote
  #4  
Old September 24th, 2002, 09:23 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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)

Reply With Quote
  #5  
Old September 24th, 2002, 11:20 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: File System commands in php

fopen("/home/abc/test.txt", 'w+');

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > File System commands in php


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT