Results 1 to 8 of 8

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

  1. #1
    Join Date
    Apr 2013

    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!



    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


    <!doctype html>
    <title>YOUR ALUMNI FORM</title>
    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;}

    <body style="margin-left:200px;">

    <br />
    <br />

    <form method="post" action="1.bio.processor.php" enctype="multipart/form-data" border="0">

    <label><span style="color:red; font-weight:bold;">*</span>First Name</label>
    <input type="text" name="firstname" />

    <label><span style="color:red; font-weight:bold;">*</span>Last Name</label>
    <input type="text" name="lastname" />

    <label><span style="color:red; font-weight:bold;">*</span>Your E-Mail</label>
    <input type="text" name="email" />

    <label>Your Clubs</label>
    <input type="text" name="clubs" />

    and so forth...

    <div style="margin-left:390px;"><input style="width:75px; height:24px;" type="submit" name="submit" value="Submit" /></div>




    //name of the template file
    $tpl_file = "1.bio.template.php";

    //determine if form has been submitted

    //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');




    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);
    return $bytes;

  2. #2
    Join Date
    Aug 2004
    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');
    $f == false) {
    error_log("Unable to open '$template_file_name' for writing");
    "<p class='error'>Sorry, there was an unrecoverable error.</p>");

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 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!


  4. #4
    Join Date
    Mar 2011
    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
    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.


  6. #6
    Join Date
    Aug 2004
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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

  8. #8
    Join Date
    Aug 2004
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12654 seconds
  • Memory Usage 2,941KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates