/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] File Put Contents Workaround for PHP Version 4 Not Working

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

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 17.2013 — Firstly, any web host that cannot or will not run PHP 5 in this day and age is [b]way[/b] behind the times, and I would suggest moving to some other hosting company ASAP.

Anyway, get rid of the "@" before fopen() (at least for now) so that PHP will tell you if something is going wrong there. Once you get something working, you can put it back in, but then make sure you're catching any errors and handling them appropriately if something does go wrong getting the file handle.
[code=php]
$f = @fopen($template_file_name, 'w');
if($f == false) {
error_log("Unable to open '$template_file_name' for writing");
die("<p class='error'>Sorry, there was an unrecoverable error.</p>");
}
[/code]
Copy linkTweet thisAlerts:
@qwertyportneauthorApr 17.2013 — Thanks No Dog, wish I could convincve the webmaster to do exactly that but either he can't or won't. I developed these scripts for my friend who is responsible for handling user profiles and photos. I used my local (XAMMP) and remote (my personal website) servers (both PHP version 5.3) to get everything working then discovered my friend's server is 4.3. Funny thing is that using file_put_contents results in the error message whereas the workaround moves right on to the photo upload form as if the profile has been saved, but when I go to the folder where the profile should have been saved there is nothing there. I've changed the permissions to 777, tried different doctypes and a bunch of other stuff. I'll try your changes to see if I can get some errors to redirect my approach. Meanwhile do you or anybody else reading this thread know of any other alternatives to file_put_contents that might work with version 4? I've been at this for 7 weeks and it's driving me nuts!

Bill
Copy linkTweet thisAlerts:
@rtretheweyApr 17.2013 — I'd suggest adding the full path to the destination directory to your variable $template_file_name. The current directory may not be the same on all of the systems you're using to test this script.
Copy linkTweet thisAlerts:
@qwertyportneauthorApr 17.2013 — 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
Copy linkTweet thisAlerts:
@NogDogApr 18.2013 — Ah...I see now. Yes, if you put a function (or class) definition within any kind of conditional block, it will not get defined at compile time, instead only being defined at run-time if/when that condition is actually triggered.
Copy linkTweet thisAlerts:
@qwertyportneauthorApr 18.2013 — How do I mark this RESOLVED?
Copy linkTweet thisAlerts:
@NogDogApr 18.2013 — How do I mark this RESOLVED?[/QUOTE]
See the "Thread Tools" option above the first post on the page.
×

Success!

Help @qwertyportne spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...