Avatar of JamesFrog
JamesFrog

asked on 

Fatal error: Call to undefined function: file_put_contents() when using file_put_contents()

Hi,

I keep getting "Fatal error: Call to undefined function: file_put_contents()...." errors with the following code


<?
// START BUFFERING
ob_start();

//Require the mysql connection information
require_once ('../../mysql_sha_connect.php');

switch ($button)
                {        
                        case "Publish product to WEB":
            
//get the sha_ID
$sha_ID = $_POST['sha_ID'];
//get the publishable content form the form      
$query = "SELECT * FROM sha WHERE sha_ID='$sha_ID'";      
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo $row['HTML_SALESCONTENT'].$row['HTML_CONTENT'].$row['HTML_SALESCONTENT'];

// ACQUIRE THE CONTENTS OF THE BUFFER
$BUFFER_contents = ob_get_contents();

// DISPOSE OF THE BUFFER
ob_end_clean();

// WRITE THE FILE TO THE SERVER
$file_location = "/home/shauser8/web".$row['HTML_pagefolder'].$row['HTML_pagename'];
file_put_contents($file_location, $BUFFER_contents);
 
 }
?>

What am I doing wrong?

Thanks James
PHP

Avatar of undefined
Last Comment
JamesFrog
Avatar of glcummins
glcummins
Flag of United States of America image

The function file_put_contents is available in PHP 5, but not earlier versions. Are you running at least version 5 of PHP?
Avatar of JamesFrog
JamesFrog

ASKER

No it's PHP Version 4.4.8,  what's the alternative to carry this out with PHP Version 4.4.8 then please?
Avatar of JamesFrog
JamesFrog

ASKER

Does this code look good to do this with 4.4.8 or is there a better way?

________________________
$filename='newfile.php';
$mystring='text';
if (($fh = @fopen($filename, "w")) !== false)
{
    if (@fwrite($fh, $mystring) === false)
    {
        die(sprintf("Unable to write to file: %s!", $filename));
    }
    fclose($fh);
}
else
{
    die(sprintf("Could not open file %s for writing!", $filename));
}  
_______________________________________
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of glcummins
glcummins
Flag of United States of America image

The only differences between our two functions are the return values, and the die() statement in yours. Generally in single-purpose functions, you do not want to use die statements. Instead, check for a faulty return value, and take a logical action in your code.
Avatar of JamesFrog
JamesFrog

ASKER

Will your code above CREATE the file name in the server (or does the file have to already exist on the server with write permissions set for this to work?)

I need to code to actually CREATE the file on the server as opposed to 'writing over' an existing file on the server.

Thanks
James
Avatar of Ali Kayahan
Ali Kayahan
Flag of Türkiye image

The one below will create file each time (it will cause error for overwrite existing file) if you check the file name with file_exists() and change mode x+ to a+ , than it will append to the existing file...

<?php
if (!function_exists('file_put_contents'))
{
   function file_put_contents($filename, $data)
   {
      $f = @fopen($filename, 'x+');
      if (!$f)
      {
         return false;
      }
      else
      {
         $bytes = fwrite($f, $data);
         fclose($f);
         return $bytes;
      }
   }
}
?>

Open in new window

Avatar of glcummins
glcummins
Flag of United States of America image

The 'w' flag causes the file to be created if it does not exist. For more information on the available fopen flags, please see:

http://www.php.net/fopen/
Avatar of JamesFrog
JamesFrog

ASKER

The above creates the file perfectly.. however..

1) How does the above need to be modified so that the file is just re-written on the server if the same filename (and location) is used. At the moment it doesn't do this.

2) How do I then delete the file from the server?

Thanks
James
Avatar of glcummins
glcummins
Flag of United States of America image

The 'w' flag already tells PHP to rewrite the file if it already exists.

To delete an existing file, you can use the unlink() function: http://www.php.net/unlink
Avatar of JamesFrog
JamesFrog

ASKER

Thankyou. I work out how to you unlink - your code works perfectly
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo