![]() |
Using fopen function....
Hey guys...
I am currently using shell_exec to create/add data to about 5 different files. I'd like to get away from shell_exec and use fopen. I'm trying to be a better programmer so what is the best way to use fopen if I need to add data to 5 different files? Do I just simply open/close handle, open/close handle literally 5 times? Or should I create some kind of function for it? I don't need to read the file. If the file exists, write to it, if not, create it. If there's data, write over it. That's why I will be using "w". $fp = fopen("/usr/local/apache/htdocs/dev01.txt","w") or die ("some message"); Here's what I am doing currently... (don't worry about what it is that I'm doing, it's a LONG story) Code:
So I basically want to replace the above code with fopen function.... |
Soemthing like this may help
PHP Code:
|
Quote:
Thanks Krik, I will give that a shot.... My program is about 1000 lines long. Towards the end is where I use the shell_exec... It is all procedural, no OOP at all.... So can I create the function on the top of the program and use the WritetoFile() function anywhere in the script? |
Quote:
|
Sounds like file_put_contents could help you.
PHP Code:
|
Quote:
Ohhh even better! I didn't know about this function. Let me look into it.... |
Ok file_put_contents is EXACTLY what I was looking for.
I apologize about using fopen as i wasn't aware of this function. But at least now I know how to use fopen with multiple files!! |
yea, file_put_contents is one of those great stream wrappers introduced in PHP5. file_get_contents is also very useful.
|
Quote:
Yeah, that's what's kind of pathetic of me, I am actually using file_get_contents in my script ironically, and I never thought about file_put_contents!!! |
You know what they say: only experience makes you better :-D
|
Quote:
|
| All times are GMT. The time now is 12:56 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0