Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Workaround for file_put_contents not working

$
0
0

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

 


Viewing all articles
Browse latest Browse all 13200

Latest Images

Trending Articles



Latest Images