Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Workaround for file_put_contents NOT Working

Links for php scripts

Moderators: macek, egami, gesf

Workaround for file_put_contents NOT Working

Postby qwertyportne » Wed Apr 17, 2013 8:04 am

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

Code: Select all
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...

Code: Select all
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;
        }
    }
}
qwertyportne
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Apr 17, 2013 7:42 am

RESOLVED: Workaround for file_put_contents NOT Working

Postby qwertyportne » Wed Apr 17, 2013 1:59 pm

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
qwertyportne
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Apr 17, 2013 7:42 am


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron