Read/Write text file from browswer using PHP

Hello All,

I have some Raspberry Pi’s that I have wired up to some 40x2 character LCDs. The LCDs display messages held in a text file. If I want to change the message I have to ssh in to the Pi and edit the text file. I thought it would be cool to do this from a web interface, and found something that looks perfect (not my code) but it does not seem to work, however it is from 2004 and I am guessing syntax and such has possibly changed.

The code I am using should write to and read from a file called my_data.txt, but doesn’t seem to do anything, I don’t get any errors from the browser either.

Platform:
Raspberry Pi 3
Raspbian Jesse
PHP5
Apache 2

I have tried to make the www-data the file/folder owner, and used chmod 777 to ensure that their are no rights issues.

I know it’s something I have done/not done, but any help would be greatly appreciated.

Code:
[php]!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

Text Editor External Text Editor

Click on "Get" to display the current text file contents.
Enter text and click on "Submit" to update the text file.
Type ‹br›<$
The textfile currently contains the following text:


<?php if (isset($gettext)){ $myfile = fopen("my_data.txt","r"); $mydata = fread($myfile,filesize("my_data.txt")); print $mydata; } else if (isset($submit)){ $myfile = fopen("my_data.txt","w+"); $entry = ereg_replace("\n"," ",$entry); $entry = stripslashes($entry); $mydata = "&mytext= $text_file &\n\n"; fwrite($myfile,$mydata); fclose($myfile); $myfile = fopen("my_data.txt","r"); $mydata = fread($myfile,filesize("my_data.txt")); print $mydata; } ?>

[/php]

Does it change the file contents?

Hello,

No it does not, and now I have a feeling that I have misunderstood this file, because when I hover over the buttons, the buttons seem to link back to the same file (so I am running this file as edittext.php, and the buttons, when you hover over them say http://ipaddress/edittext.php, so I think clicking the buttons is just reloading the page?

It’s a self contained program, that isn’t the issue.

The question is whether line 40 actually executes or has an exception thrown, that you don’t know about.

Im not sure, I tried to debug it in IE by using the view code options, but couldn’t seem to see anything useful.

If I create a file manually, it still doesn’t have any data added, nor does a populated file show on the web interface when you try to view it.

:frowning:

I don’t have any spare SD Cards to test in my Pi’s, but I would suggest updating the code to HTML5 and changing a few things on the page as well.

This works for me, and is mobile friendly, so you can also do this from a phone.

[php]<?php
$file = “my_data.txt”;

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

try {
    $myfile = fopen($file, 'w+');
    if (! file_put_contents($file, htmlentities($_POST['text_file']))) {
        $output = "2. Error: Could not write to file.";
    }
} catch (Exception $e) {
    $output = "3. Error: " . $e->getMessage();
}

}

try {
$myfile = fopen($file, “r”);
$output = fread($myfile, filesize($file));
} catch (Exception $e) {
$output = '1. Error: ’ . $e->getMessage();
}

?>

Text Editor

Pi Text Editor

<?php echo isset($output) ? $output : ''; ?>
		</div>
	</form>

</div>
<script
	src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js'></script>
<script
	src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js'></script>
[/php]

Hello, thank you for showing me this.

I saved the code as test.php. when I access it via a web browser, I see a title Pi Text Editor, I enter some text and hit submit, but I don’t see any files being created, where is the text supposed to end up (test_file?)

Best regards.

It assumes there is already a file to read and write from/ to.

I would change the owner of the file to the user that logs in when the pi is started though.

Brilliant, I created the my_data.txt file manually, and now it works perfectly, thank you :slight_smile:

Best regards.

Sponsor our Newsletter | Privacy Policy | Terms of Service