TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-19-2009, 01:31 AM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default 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....
allworknoplay is offline  
Reply With Quote
Old 03-19-2009, 02:18 AM   #2 (permalink)
The Contributor
 
Join Date: Feb 2009
Posts: 65
Thanks: 0
Krik is on a distinguished road
Default

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);
?>
Krik is offline  
Reply With Quote
The Following User Says Thank You to Krik For This Useful Post:
allworknoplay (03-19-2009)
Old 03-19-2009, 02:30 AM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Krik View Post
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?
allworknoplay is offline  
Reply With Quote
Old 03-19-2009, 02:52 AM   #4 (permalink)
The Contributor
 
Join Date: Feb 2009
Posts: 65
Thanks: 0
Krik is on a distinguished road
Default

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
Krik is offline  
Reply With Quote
The Following User Says Thank You to Krik For This Useful Post:
allworknoplay (03-19-2009)
Old 03-19-2009, 11:12 AM   #5 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

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.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
allworknoplay (03-19-2009)
Old 03-19-2009, 03:27 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
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 is offline  
Reply With Quote
Old 03-19-2009, 05:11 PM   #7 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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!!
allworknoplay is offline  
Reply With Quote
Old 03-20-2009, 11:15 AM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

yea, file_put_contents is one of those great stream wrappers introduced in PHP5. file_get_contents is also very useful.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
allworknoplay (03-20-2009)
Old 03-20-2009, 03:19 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
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!!!
allworknoplay is offline  
Reply With Quote
Old 03-21-2009, 07:16 AM   #10 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You know what they say: only experience makes you better
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 03-21-2009, 08:19 PM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
You know what they say: only experience makes you better
Absolutely true and I'm trying to soak all of it up as much as possible.....
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
The hello world contest Village Idiot The Lounge 9 01-07-2008 02:15 PM


All times are GMT. The time now is 07:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design