Simple PHP to modify text file

Hi there! I know literally NOTHING about PHP - I’m currently learning Python, and whilst I do want to learn PHP and others, I don’t really want to do too many things at once. That said, I’ve got a home automation project on a Raspberry pi, which works great - it takes it’s on and off times from a local text file. At the moment, I modify that text file using SFTP but I want to put a really simple web interface on the pi, just to modify this text file.

I’ve got Apache and PHP working. I know my way around HTML but my Javascript is VERY rusty, I haven’t written any for 15 years. All I want to do is have a webpage which displays a textbox containing the current file contents, and let’s me edit it. Basically I want a web-based notepad, nothing more for now.

I found this: http://www.dynamicdrive.com/forums/showthread.php?4539-how-do-i-modify-existing-txt-files-with-php (which was admittedly probably written before some of you were born!!) - I liked it because it’s so short I can pick through it line by line, but it doesn’t work (possibly because versions have moved on since then, I don’t know). The posts I find on StackExchange and such tend to say “you ned to learn PHP, CSS, JQuery, COBOL, Fortran, Sanscrit and Heiroglyphics to do this” but I want to keep things as simple as possible.

If it’s any help, security is not an issue whatsoever (for now, at least) as my pi is not connected to the Internet.

this is so wrong (and insecure) - except for reading and writing a textfile by anyone somewhere

<?php
if (isset($_POST['text'])) file_put_contents('file.txt', $_POST['text']);
?>
<form method="post">
    <textarea name="text"><?=file_get_contents('file.txt')?></textarea><br/>
    <input type="submit"/>
</form> 
1 Like

This appears to work.

The most important part about this is that it WORKS - I don’t intend to use it as-is, but the fact that it works means I have something to work with. I can now make it not work, and then make it work again, and learn accordingly.

You, sir, have literally made my day. Thank you. If I ever happen to become a billionaire software developer, I promise you that I will track you down and give you 10% of my company.

(Well, maybe 5%. Don’t get selfish…)

Seriously, THANK YOU!!

Sponsor our Newsletter | Privacy Policy | Terms of Service