Brydon McCluskey
Brydon McCluskey

Reputation: 50

PHP Creating and editing TXT file

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.

Upvotes: 0

Views: 2074

Answers (4)

thaJeztah
thaJeztah

Reputation: 29077

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

Upvotes: -1

Grmn
Grmn

Reputation: 544

<?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);
?>

Upvotes: 0

castis
castis

Reputation: 8223

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

I recommend WAMP

Upvotes: 1

Syndrose
Syndrose

Reputation: 112

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.

Upvotes: 1

Related Questions