TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Using fopen function.... (http://www.talkphp.com/absolute-beginners/4057-using-fopen-function.html)

allworknoplay 03-19-2009 01:31 AM

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:


shell_exec("echo \"$dev01\" > /usr/local/apache/htdocs/$dev01.txt");

shell_exec("echo \"$dev02\" > /usr/local/apache/htdocs/$dev02.txt");

shell_exec("echo \"$dev03\" > /usr/local/apache/htdocs/$dev03.txt");

shell_exec("echo \"$dev04\" > /usr/local/apache/htdocs/$dev04.txt");

shell_exec("echo \"$dev05\" > /usr/local/apache/htdocs/$dev05.txt");


So I basically want to replace the above code with fopen function....

Krik 03-19-2009 02:18 AM

Soemthing like this may help
PHP Code:

<?php
function WritetoFile($file$content) {
    
$fp fopen($file'w');
    
fwrite($fp$content);
    
fclose($fp);
}

$newcontent 'The new content for myfile.txt';
WritetoFile('myfile.txt'$newcontent);
?>


allworknoplay 03-19-2009 02:30 AM

Quote:

Originally Posted by Krik (Post 22352)
Soemthing like this may help
PHP Code:

<?php
function WritetoFile($file$content) {
    
$fp fopen($file'w');
    
fwrite($fp$content);
    
fclose($fp);
}

$newcontent 'The new content for myfile.txt';
WritetoFile('myfile.txt'$newcontent);
?>



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?

Krik 03-19-2009 02:52 AM

Quote:

So can I create the function on the top of the program and use the WritetoFile() function anywhere in the script?
Yep that will work

xenon 03-19-2009 11:12 AM

Sounds like file_put_contents could help you.

PHP Code:

file_put_contents('filename.txt''file contents...'); 

Why create your own function when the language itself provides you with one? I just don't see the point, unless you want to append to an existing file.

allworknoplay 03-19-2009 03:27 PM

Quote:

Originally Posted by xenon (Post 22358)
Sounds like file_put_contents could help you.

PHP Code:

file_put_contents('filename.txt''file contents...'); 

Why create your own function when the language itself provides you with one? I just don't see the point, unless you want to append to an existing file.


Ohhh even better! I didn't know about this function.

Let me look into it....

allworknoplay 03-19-2009 05:11 PM

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!!

sketchMedia 03-20-2009 11:15 AM

yea, file_put_contents is one of those great stream wrappers introduced in PHP5. file_get_contents is also very useful.

allworknoplay 03-20-2009 03:19 PM

Quote:

Originally Posted by sketchMedia (Post 22383)
yea, file_put_contents is one of those great stream wrappers introduced in PHP5. file_get_contents is also very useful.


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!!!

xenon 03-21-2009 07:16 AM

You know what they say: only experience makes you better :-D

allworknoplay 03-21-2009 08:19 PM

Quote:

Originally Posted by xenon (Post 22414)
You know what they say: only experience makes you better :-D

Absolutely true and I'm trying to soak all of it up as much as possible.....


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