TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   fopen (http://www.talkphp.com/absolute-beginners/3157-fopen.html)

Tanax 07-21-2008 09:08 PM

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!

delayedinsanity 07-21-2008 11:15 PM

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

Tanax 07-21-2008 11:25 PM

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.. =//

delayedinsanity 07-22-2008 01:00 AM

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+');



Kalle 07-22-2008 09:55 AM

Are you working with PHP4 since you use a function called file_put_contents() cause in PHP5 theres one called that (=

Tanax 07-22-2008 01:22 PM

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);

    } 



All times are GMT. The time now is 11:46 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0