Jump to content

Workaround for file_put_contents not working


qwertyportne
Go to solution Solved by qwertyportne,

Recommended Posts

Hello to the forum. I'm just learning PHP and thought it wise to use a real project as a place to start. My friend needs a script to automatically handle user profile and photo uploads. The first script below is what I finally put together. It works on my local and remote servers running PHP version 5.3 but my friend's server running PHP version 4.4 yields an undefined function for the file_put_contents code. A little research told me PHP version 4 does not recognize that function.

His webmaster either cannot or will not upgrade to PHP version 5. Strange but nothing I can do now to change that. So I went looking for a workaround for the file_put_contents function. The one I tried is the second code below. I no longer get the error and the script moves on to the photo upload page as if the profile has been saved, but when I go up to his server with my FTP program it has not been saved. I'd appreciate any help the forum could offer. I've been at this for weeks now and pretty much figure I'm the reason it won't work because I'm still wet behind the ears when it comes to php stuff. Thanks everyone,

~Bill

 

error_reporting(E_ALL);
ini_set('display_errors','on');
//name of the template file
$tpl_file = "1.bio.template.php";
//determine if form has been submitted
if($_SERVER['REQUEST_METHOD'] == 'POST'){
 //very simple form filter and validation
 $data = array();
 $data['firstname'] = trim(strip_tags($_POST['firstname']));
 $data['lastname'] = trim(strip_tags($_POST['lastname']));
 $data['email'] = trim(strip_tags($_POST['email']));
 $data['clubs'] = trim(strip_tags($_POST['clubs']));
 $data['years'] = trim(strip_tags($_POST['years']));
 $data['bikes'] = trim(strip_tags($_POST['bikes']));
 $data['classes'] = trim(strip_tags($_POST['classes']));
 $data['number'] = trim(strip_tags($_POST['number']));
 $data['hobbies'] = trim(strip_tags($_POST['hobbies']));
 $data['highlights'] = trim(strip_tags($_POST['highlights']));
 $data['comments'] = trim(strip_tags($_POST['comments']));
 $placeholders = array("{firstname}", "{lastname}", "{email}", "{clubs}", "{years}", "{bikes}", "{classes}", "{number}", "{hobbies}", "{highlights}", "{comments}");
// Establish required entries -- first and last name become filename for users bio
if(!empty($data['firstname']) && !empty($data['lastname']) && !empty($data['email']) ){
 //load the template file
  $tpl = file_get_contents($tpl_file);
 //replace placeholders with the submited data
  $template_html = str_replace($placeholders, $data, $tpl);
 //build the template using last name and first as file name
  $template_file_name = $data['lastname']."_".$data['firstname'].".htm";
 //create the file with form contents
  file_put_contents($template_file_name, $template_html);
 //display photo upload form
  header('location: 2.photo.upload.php');
  exit;
}
}

And here's the workaround that supposedly works in PHP version 4...

 

if (!function_exists('file_put_contents')) {
    function file_put_contents($template_file_name, $template_html) {
        $f = @fopen($template_file_name, 'w');
        if (!$f) {
            return false;
        } else {
            $bytes = fwrite($f, $template_html);
            fclose($f);
            return $bytes;
        }
    }
}

 

Link to comment
Share on other sites

I'm guessing your PHP script doesn't have access to create a new file in the directory specified. You are suppressing errors on the fopen, so it is probably just going to the next statement and returning false. Take the "@" symbol away and see if you get an error.

Link to comment
Share on other sites

 

 

And here's the workaround that supposedly works in PHP version 4...

 

if (!function_exists('file_put_contents')) {
    function file_put_contents($template_file_name, $template_html) {
        $f = @fopen($template_file_name, 'w');
        if (!$f) {
            return false;
        } else {
            $bytes = fwrite($f, $template_html);
            fclose($f);
            return $bytes;
        }
    }
}

 

 

 

The logic of that function is completly wrong.

 

You can not use it in this way.

Link to comment
Share on other sites

You can not use it in this way.

 

 

Though unorthodox and cumbersome, it IS syntactically correct. PHP allows you to put function definitions inside of conditional statements.

 

Thinking about it now, maybe  he is not calling the function after defining it.

 

@qwertyportne,

 

Did you replace the call to file_put_contents() with that code? It shouldn't replace the code, but actually be placed above it instead.

Link to comment
Share on other sites

  • Solution

Thanks everyone, the problem is solved. I had to put the workaround at the very beginning of my existing code. Would not work at the end or just before the file_put_contents. Wow, what a project this has been. But my friend should be happy. I know I am. Closure... ~Bill

 

PS How do I mark this resolved?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.