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 07-21-2008, 09:08 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default fopen

Hey!

I'm having some trouble with a function:
PHP Code:
    /**
    * Function file_put_contents
    * Params $szFilename, $szInject
    *
    * Writes the text into the logfile. New text is always added in the beginning of logfile so that newest is always on top
    * Returns true or false
    **/
    
function file_put_contents($szFilename$szInject) {
        
        
// Open the file
        
$szContent fopen($szFilename'w+');
        
        
// Write the text into it
        
$put fwrite($szContent$szInject);
        
        if(
$put) {
            
            
// If success, return true
            
return true;
            
        }
        
        
// If failure, return false
        
return false;

    } 
Problem: Well it seems to overwrite the content of the file, so everytime the function is used, the file is replaced with a new content, and not ADDING the content.

I want the marker at the beginning of the file when opened so the newest text is written in the beginning. I also want it to CREATE the file if the file does not exist.

Thanks in advance!
__________________
Tanax is offline  
Reply With Quote
Old 07-21-2008, 11:15 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Without looking into fopen flags, here's what I would do off the top of my head:

PHP Code:
function file_put_contents($szFilename$szInject) {

    
$szInput file_get_contents($szFilename);
    
    
$szOutput fopen($szFilename'w+');

    
$szInput $szInject."\n".$szInput;

    
$put fwrite($szContent$szInput);

    return (
$put == TRUE) ? TRUE FALSE;


Now after looking into fopen flags...

'w+' won't work, because as the manual says: Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

Your function will probably work just the way it is if you change w+ to r+, like so:

PHP Code:
    function file_put_contents($szFilename$szInject) { 
         
        
// Open the file 
        
$szContent fopen($szFilename'r+'); 
         
        
// Write the text into it 
        
$put fwrite($szContent$szInject); 
         
        if(
$put) { 
             
            
// If success, return true 
            
return true
             
        } 
         
        
// If failure, return false 
        
return false

    } 
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Tanax (07-21-2008)
Old 07-21-2008, 11:25 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

The first one without flags will probably be the best shot, why didn't I think of that...?

The one with r+ won't work, because r+ does not attempt to create it if it doesn't exist.. =//
__________________
Tanax is offline  
Reply With Quote
Old 07-22-2008, 01:00 AM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Right, I forgot that part.

You could always check file_exists first,

PHP Code:
if (file_exists($szFilename))
{
    
fopen($szFilename'r+');
}
else
{
    
fopen($szFilename'w+');

delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Tanax (07-22-2008)
Old 07-22-2008, 09:55 AM   #5 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Are you working with PHP4 since you use a function called file_put_contents() cause in PHP5 theres one called that (=
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
Tanax (07-22-2008)
Old 07-22-2008, 01:22 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I got it working:

PHP Code:
/**
    * Function file_intersperse_contents
    * Params $szFilename, $szInject
    *
    * Writes the text into the logfile. New text is always added in the beginning of logfile so that newest is always on top
    * Returns true or false
    **/
    
function file_intersperse_contents($szFilename$szInject) {
        
        
// Check if file exist
        
if(file_exists($szFilename)) {
            
            
// If file exist
            // Get the content
            
$szContent file_get_contents($szFilename);
            
// Add the new text in the beginning
            
$szInject $szInject $szContent;
            
// Open the file and delete all text in it
            
$szFile fopen($szFilename'w+');
            
        }
        
        else {
            
            
// If file does not exist
            // Create file and open the file
            
$szFile fopen($szFilename'a+');
            
        }
        
        
// Write the text into it
        
$put fwrite($szFile$szInject);
        
        if(
$put) {
            
            
// If success, return true
            
return true;
            
        }
        
        
// If failure, return false
        
return false;
        
        
// Close the file
        
fclose($szFile);

    } 
__________________
Tanax 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


All times are GMT. The time now is 11:07 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