Jump to content

Correct format??? or no


prcollin

Recommended Posts

$fp = fopen('userpages/' . $page_title . '.html', 'a');

 

is that the correct format for the function to pull a value($page_title) from a form in a .html file and create a directory with the form entry??

 

Getting this error but permissions are all right.  Im guessing its not pulling hte value and substituting it for the entered value.  So how would i go about doing that?

 

View my post here to see all the relavent code http://www.phpfreaks.com/forums/index.php/topic,199546.0.html

 

 

Warning: fopen(userpages/page_title.html) [function.fopen]: failed to open stream: No such file or directory in /home/findzer/public_html/userpages/createfile.php on line 13

 

 

Link to comment
Share on other sites

The problem is probably not do with the code but is more likely to do with the file path used in fopen.

 

That code is being ran from the /home/findzer/public_html/userpages directory in a file called createfile.php (as the error states).

 

Now the path you use in fopen is userpages/what-ever-file.html, As this is a relative file path PHP will add your path onto the current working directory (path above) and so the path PHP will use to open the .html file will be:

/home/findzer/public_html/userpages/userpages/what-ever-file.html

Which is obviously wrong. I'd suggest you to use:

$fp = fopen('./' . $page_title . '.html', 'a');

// OR
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/userpages/' . $page_title . '.html', 'a');

Link to comment
Share on other sites

The problem is probably not do with the code but is more likely to do with the file path used in fopen.

 

That code is being ran from the /home/findzer/public_html/userpages directory in a file called createfile.php (as the error states).

 

Now the path you use in fopen is userpages/what-ever-file.html, As this is a relative file path PHP will add your path onto the current working directory (path above) and so the path PHP will use to open the .html file will be:

/home/findzer/public_html/userpages/userpages/what-ever-file.html

Which is obviously wrong. I'd suggest you to use:

$fp = fopen('./' . $page_title . '.html', 'a');



// OR
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/userpages/' . $page_title . '.html', 'a');

 

 

 

Great no more errors, but now i just get a blank page when i click submit and there is no new file in the directory.

Link to comment
Share on other sites

Is your error_reporting to set to E_ALL? PHP should display all error types and not a blank page if error_reporting is set to E_ALL.

 

There are actually no errors but when i added

 

echo $page_title;

 

it printed page_title when i submitted the form so does this mean that the variables are not being recognized and if so should I just put my php script at the bottom of the form or should i declare the variables some other way?

Link to comment
Share on other sites

Looking back at your original thread your script is not being instructed to output anything, thus you get a blank page when form is submited.

 

As for no file being created then you you should add

fclose($fp);

at the bottom of your script before ?> in createfile.php

Link to comment
Share on other sites

Looking back at your original thread your script is not being instructed to output anything, thus you get a blank page when form is submited.

 

As for no file being created then you you should add

fclose($fp);

at the bottom of your script before ?> in createfile.php

 

new code

 

<?php

/** 
* 
*This script posts the new file to the directory 
* 
* (c) copyright 2008 Time Vision Development 
*/

$page_title = 'page_title';
$user_email = 'user_email';
$about_you = 'about_you';

$fp = fopen('./' . $page_title . '.html', 'a');
fwrite($fp, '<b>Email Address:</b>');
fwrite($fp, '<br>');
fwrite($fp, $_POST[$user_email]);
fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<b>About Me:</b>');
fwrite($fp, '<br>');
fwrite($fp, $_POST[$about_you]);
fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<hr height="5" width="100%">');

echo $page_title;
echo $user_email;
echo $about_you;

fclose($fp);

?>

Link to comment
Share on other sites

Try:

<?php

if(isset($_POST['submit']))
{
    $path = $_SERVER['DOCUMENT_ROOT'] . '/userpages/';

    $file_name     = $_POST['page_title'];

    $file_contents = "<b>Email Address:</b>
<br>
{$_POST['user_email']}
<br />
<br />
<b>About Me:</b>
<br />
{$_POST['about_you']}
<br />
<br />
<hr height=\"5\" width=\"100%\">";

    // Let's make sure the file exists and is writable first.
    if (is_writable($path))
    {
        // In our example we're opening $filename in append mode.
        // The file pointer is at the bottom of the file hence
        // that's where $somecontent will go when we fwrite() it.
        if (!$handle = fopen($path . $file_name, 'a'))
        {
             echo "Cannot open file $file_name";
             exit;
        }

        // Write $somecontent to our opened file.
        if (fwrite($handle, $file_contents) === FALSE)
        {
            echo "Cannot write to file $file_name";
            exit;
        }

        echo "Success, created $file_name";

        fclose($handle);

    }
    else
    {
        echo "The file $path is not writable";
    }
}
else
{
    echo 'Form not submitted! $_POST contents:';
    echo '<pre>' . print_r($_POST, true) . '</pre>';
    
}

?>

This is will print messages at different stages. It should identify where your script is failing.

Link to comment
Share on other sites

These files are being written to the directory on submit they are not already in the directory each time the form is submitted a new file is supposed to be created with the form elements as the content and the &page_title as the document name

Link to comment
Share on other sites

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.