www.webdeveloper.com
Results 1 to 8 of 8

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

  1. #1
    Join Date
    Apr 2013
    Posts
    8

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

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Firstly, any web host that cannot or will not run PHP 5 in this day and age is way 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.
    PHP Code:
    $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>");

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2013
    Posts
    8
    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

  4. #4
    Join Date
    Mar 2011
    Posts
    1,137
    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.
    Rick Trethewey
    Rainbo Design

  5. #5
    Join Date
    Apr 2013
    Posts
    8
    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

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Apr 2013
    Posts
    8
    How do I mark this RESOLVED?

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Quote Originally Posted by qwertyportne View Post
    How do I mark this RESOLVED?
    See the "Thread Tools" option above the first post on the page.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles