0

I have tried everything, and multiple different source codes, I have tried creating the file so all the PHP had to do was write in it, and still unable to do it.

This is my HTML on index.html

<form name="userinput" action="usersave.php" method="post">
Your name: <input type="text" name="username><br>
Your email: <input type="text" name="useremail"><br>
Your story: <textarea rows="3" cols="50" name="userstory"></textarea><br>
<input type="submit" value="Submit!" name="submit">
</form>

And this is my PHP on usersave.php

<header>
<?php
    $file = 'output.txt';
    $buffer = $_POST['userstory'];

    if (file_exists($file)) {
            $buffer = file_get_contents($file) . "\n" . $buffer;
    }

    $success = file_put_contents($file, $buffer);
?>
</header>

any help is appreciated, please and thank you.

4
  • Any errors or pertinent log file information? Commented Apr 5, 2013 at 23:12
  • Have you got some permissions errors? Commented Apr 5, 2013 at 23:12
  • I am not using any host whatsoever and am opening the files directly from my PC into Chrome. Unsure about any of the above. Commented Apr 5, 2013 at 23:14
  • You mean, Windows Explorer > YourFolder > thisfile.php > Open File? Commented Apr 5, 2013 at 23:17

4 Answers 4

1

edit: You'll need to install some sort of server environment to get started.

I recommend WAMP

Sign up to request clarification or add additional context in comments.

3 Comments

I am not using any host whatsoever and am opening the files directly from my PC into Chrome. Unsure about any of the above.
Your using windows I assume (chown -R www-data:www-data /dir/to/www/ is not for windows) hold on.. ill post you an exmaple code on how to read / write / to file using php
Edited my answer for windows
1

If you are not using any host what soever, then there isn't a server to run the php, the browser doesn't parse PHP, the server does, so if there isn't a server, none of the PHP gets parsed.

3 Comments

Thank you very much, can't believe it was that simple.
wait, you're just trying to open the php file in chrome?
see my answer now, you should checkout WAMP
0
<?php
$file_path = "text_file.txt";
$file_handle = fopen($file_path, 'r'); // OPEN FILE READY FOR 'R'eading!
$text_data = fread($file_handle, filesize($file_path));  //Read actual data in file
print $text_data; // print the data you can use echo if you like
fclose($file_handle); // Close the file read / buffer


$data_to_write = "This is the data that will be written to disk!";
$file_path = "new_file.txt";
$file_handle = fopen($file_path, 'w');  // OPEN FILE READY FOR 'W'riting!
fwrite($file_handle, $data_to_write);
fclose($file_handle);
?>

2 Comments

as much as this answer is good, i believe we just established that he's not actually running apache or anything, just trying to open the files in chrome...
Yea, realized that after i created the example code. He does not have apache / php installed whatsoever... however, guess he'll come back with this issue, cause his code won't do what he wants it to do
-1

I am not using any host whatsoever and am opening the files directly from my PC into Chrome.

You cannot run PHP files without a server (e.g. Apache). PHP files are 'server-side' scripts; the script is executed on the server, and the resulting output is send to the browser by the server.

See this question:

Parsing PHP content in HTML without Webserver

Or try searching Google

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.