Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 466
0 Users 6 Guests Online

Online Notepad

In this tutorial we will create a online notepad script, this will demonstrate how to open and write to a file in PHP.

1. Create a blank .txt file called notepad within the same directory as you will create the PHP script below.
    Make sure this file has permission to be wrote to.

2. Copy below and save it as notepad.php

notepad.php Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$file = "notepad.txt"; // The .txt file we created above
 
if($_GET['action'] == "edit") {
    $fh = fopen($file, 'w') or die("can't open file"); // Open the file with w (Write) permission.
    fwrite($fh,stripslashes($_POST['edit'])); // Write the contents of the textarea to the file.
    fclose($fh); // Close the file as we have finished editing it
}
 
echo "<form method='post' action='notepad.php?action=edit'><textarea cols='60' rows='20' name='edit'>";
$fh = fopen($file, 'r'); // Open the .txt file using fopen
echo fread($fh, filesize($file)); // Output the contents of the text file
fclose($fh); // Close the file as we have finished with it
echo "</textarea><br><input type='submit' value='Update'></form>";
?>
DanielXP
Author:
Views:
4730
Rating:
There are currently no comments for this tutorial.