qwertyportne Posted April 17, 2013 Share Posted April 17, 2013 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/277066-workaround-for-file_put_contents-not-working/ Share on other sites More sharing options...
lemmin Posted April 17, 2013 Share Posted April 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277066-workaround-for-file_put_contents-not-working/#findComment-1425417 Share on other sites More sharing options...
jazzman1 Posted April 17, 2013 Share Posted April 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277066-workaround-for-file_put_contents-not-working/#findComment-1425445 Share on other sites More sharing options...
lemmin Posted April 17, 2013 Share Posted April 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277066-workaround-for-file_put_contents-not-working/#findComment-1425447 Share on other sites More sharing options...
Solution qwertyportne Posted April 17, 2013 Author Solution Share Posted April 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 PS How do I mark this resolved? Quote Link to comment https://forums.phpfreaks.com/topic/277066-workaround-for-file_put_contents-not-working/#findComment-1425465 Share on other sites More sharing options...
Barand Posted April 17, 2013 Share Posted April 17, 2013 The button labelled "Mark Solved" looks favourite. Quote Link to comment https://forums.phpfreaks.com/topic/277066-workaround-for-file_put_contents-not-working/#findComment-1425470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.