I've been working on a basic cms script locally on my machine which I've installed php version 5 on.
When I uploaded my script, i got the error

Fatal error: Call to undefined function: file_put_contents() in /webname/CMPs/test.php on line 8

This I've since discovered is because my host runs PHP Version 4.4.2 and
file_put_contents() was not implemented until version 5.

My question is how similar is using the functions fopen(), fwrite(), and fclose() to using file_put_contents()?

This is what I'm using at the mo

file_put_contents($file_path, $file_new_contents);

would I need to write something like

fopen($file_path, "w");
fwrite($file_new_contents);
fclose($file_path);

After trying this I dont get the error anymore. Everthing now seems to be working close to how its supposed to however the contents of the original file just get deleted rather than updated. Any know why this may be?

Heres some code,

<?php 
// if the file is being saved 
if (isset($_POST['submit'])) { 
    $file_path = $_POST['file_path']; 
    $file_new_contents = $_POST['file_new_contents']; 
	fopen($file_path,"w+");
	fwrite($file_new_contents);
	fclose($file_path);
    header('Location: test.php'); // redirect back to main page after save 
} 

if (isset($_GET['dir'])) { //check to see if a link has been clicked 
    $dir = $_GET['dir']; 

switch (filetype($dir)){ //check to see if it's a directory or a file 
    case 'file':  //if its a file open for editing 
    $file_path = $dir; 
    $file_contents= file_get_contents($file_path); 
        echo "<form method=\"post\" action\"\"> 
	    <p>File being edited: ".$file_path."</p> 
	    <textarea rows=\"30\" cols=\"80\" name=\"file_new_contents\">$file_contents</textarea>"; 
	    echo "<input type=\"hidden\" name=\"file_path\" value=\"".$file_path."\"> 
        <input type=\"submit\" name=\"submit\" value=\"Save File\"> 
	    </form>"; 
    break; 

	case 'dir':  //if its a directory list the contents 
    $dir = $dir."\\"; 
    if (is_dir($dir)) { 
        if ($dh = opendir($dir)) { 
            while (($file = readdir($dh)) !== false) { 
                echo "<a href=\"test.php?dir=".$dir.$file."\" >filename: $file :</a> filetype: " . filetype($dir . $file) . "<br />\n"; 
            } 
            closedir($dh); 
        } 
    } 
    break; 
} 

} else { 
    $dir = "src/"; 

// Open a known directory, and proceed to read its contents 
if (is_dir($dir)) { 
    if ($dh = opendir($dir)) { 
        while (($file = readdir($dh)) !== false) { 
            echo "<a href=\"test.php?dir=".$dir.$file."\" >filename: $file :</a> filetype: " . filetype($dir . $file) . "<br />\n"; 
        } 
        closedir($dh); 
    } 
} 
} 

?> 

Think it would be anything to do with file_get_contents()?
I checked the manual and apparently its been available since version 4.3. So i'm ok with version 4.4.2 im guessing.

    file_put_contents():

    Identical to calling fopen(), fwrite(), and fclose() successively.

    fopen():
    Opening a file with the "w" switch loses everything in the file. If you want to append data, use "a".

      thanks!

          fopen($file_path,"a"); 

      makes it not delete stuff now, but it doesnt seem to edit anything, I've tried a+ too to see if that does anything but it doesnt. Any one got any ideas?

        ok i've realised i wasnt using fopen() properly
        gonna have a play

          Write a Reply...