My php script works fine on a server with PHP version 5 but not on a server with PHP version 4. The error indicates a problem with the file_put_contents function. A little research told me version 4 does not recognize that function. The webmaster is not able to install an upgrade, so I’ve been trying all sorts of file_put_contents work arounds but without success. Buty I’m real green at PHP, so I’d appreciate any help the forum can offer. The error message, html form, php processor and workaround I’m currently working on are below. Thanks!
~Bill
ERROR…
Fatal error: Call to undefined function: file_put_contents() in /var/www/vhosts/district37ama.org/subdomains/thebannerisup/httpdocs/alumni/1.bio.processor.php on line 45
FORM…
<!doctype html>
<html>
<head>
<title>YOUR ALUMNI FORM</title>
<style>
body {background:silver; font-family: arial; font-size:13px;}
fieldset {border:0px;}
fieldset ol li {list-style-type:none; line-height:32px;}
fieldset ol li label {width:135px; display: inline-block;}
textarea {width:500px; height:64px; rows:6;}
input {width:500px;}
</style>
</head>
<body style=”margin-left:200px;”>
<br />
<br />
<form method=”post” action=”1.bio.processor.php” enctype=”multipart/form-data” border=”0″>
<fieldset>
<ol>
<li>
<label><span style=”color:red; font-weight:bold;”>*</span>First Name</label>
<input type=”text” name=”firstname” />
</li>
<li>
<label><span style=”color:red; font-weight:bold;”>*</span>Last Name</label>
<input type=”text” name=”lastname” />
</li>
<li>
<label><span style=”color:red; font-weight:bold;”>*</span>Your E-Mail</label>
<input type=”text” name=”email” />
</li>
<li>
<label>Your Clubs</label>
<input type=”text” name=”clubs” />
</li>
and so forth…
</ol>
</fieldset>
<div style=”margin-left:390px;”><input style=”width:75px; height:24px;” type=”submit” name=”submit” value=”Submit” /></div>
</form>
</body>
</html>
FORM PROCESSOR…
<?php
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;
}
}
?>
FILE_PUT_CONTENTS WORKAROUND FOR 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;
}
}
}