Jump to content

File Put Contents Pathname


Go to solution Solved by qwertyportne,

Recommended Posts

Hello to the Forum. I need to revise this script so it saves the user's form data to a subdirectory called new rather than the root directory (where this code is now). Please note the workaround I had to add to accomodate the older version of php on the server. I've tried a few things but am too new to php to get it working. Any help I can get is appreciated. Thanks,

 

Bill

 

 

 

 
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
 
//begin workaround
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;
        }
    }
}
//end workaround

//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;
}
}
?>
 
Link to comment
https://forums.phpfreaks.com/topic/277214-file-put-contents-pathname/
Share on other sites

 

$template_file_name = $data['lastname']."_".$data['firstname'].".htm";

 

Have you tried adding the path to the variable above which you are passing to the file_put_contents() function? Something like:

 

 

$template_file_name = "subFolderName/" . $data['lastname']."_".$data['firstname'].".htm";

 

Or, my preference:

 

$template_file_name = "subFolderName/{$data['lastname']}_{$data['firstname']}.htm";
  • Solution

I got it to work this morning on my local server (XAMMP version 5,3) by adding the subfolder "new" to both the original file_put_contents function and my php version 4 workaround, as below. Was pretty happy about that, then tried it on my friend's (php version 4 something) server but no go. It took me more than a week to finally get the workaround to work because I'm just getting my head into php. Told him it was working now and have a beer. He tried it, liked it alot, but then asked me to have the new users profiles saved in a "new" subfolder so he would not confuse them with older existing profiles. Made sense to me so I have been trying to accomodate him. My suspicions are that my change is somehow stopping the workaround from working but even though I have the error reporting routine in the script I'm not getting any errors. It originally helped me discover that file_put_contents is not recognized by version 4. Hence my looking for a workaround. Yikes, what a mess. Any more ideas? I'll definitely try your suggestions though. Thanks. Should I add your suggested change to both the workaround and the file_put_contents function?

 

 

 

//begin workaround
if (!function_exists('file_put_contents')) {
    function file_put_contents($template_file_name, $template_html) {
        $f = @fopen("new/" . $template_file_name, 'w');
        if (!$f) {
            return false;
        } else {
            $bytes = fwrite($f, $template_html);
            fclose($f);
            return $bytes;
        }
    }
}
//end workaround
 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.