Create, Edit And Delete File Using PHP And HTML
Last Updated : Jul 1, 2023
In this tutorial we will show you how to create, edit and delete file using PHP and HTML. By using these methods you can create any type of file, edit any file and delete any file.
You may also like add, edit and delete records using jQuery, PHP and MySQl.
To Create, Edit And Delete File It Takes Only Two Steps:-
- Make a HTML file and define markup
- Make a PHP file to create, edit and delete file
Step 1. Make a HTML file and define markup
We make a HTML file and save it with a name file.html
<html> <body> <form method="post" action="file_operation.php" id="create_form"> <input type="text" name="file_name"> <input type="submit" value="Create File" name="create_file"> </form> <form method="post" action="file_operation.php" id="edit_form"> <input type="text" name="file_name"> <textarea name="edit_text"></textarea> <input type="submit" value="Edit File" name="edit_file"> </form> <form method="post" action="file_operation.php" id="delete_form"> <input type="text" name="file_name"> <input type="submit" value="Delete File" name="delete_file"> </form> </body> </html>
In this step we create three forms to create, edit and delete file. In first form user have to enter file name to create new file.
In second form user have to enter file name and text to add in that file.In third form user have to enter file name to delete that file.You may also like delete multiple records from MySQL using PHP.
Step 2. Make a PHP file to create, edit and delete file
We make a PHP file and save it with a name file_operation.php
<?php if(isset($_POST['create_file'])) { $file_name=$_POST['file_name']; $folder="files/"; $ext=".txt"; $file_name=$folder."".$file_name."".$ext; $create_file = fopen($file_name, 'w'); fclose($create_file); } if(isset($_POST['edit_file'])) { $file_name=$_POST['file_name']; $write_text=$_POST['edit_text']; $folder="files/"; $ext=".txt"; $file_name=$folder."".$file_name."".$ext; $edit_file = fopen($file_name, 'w'); fwrite($edit_file, $write_text); fclose($edit_file); } if(isset($_POST['delete_file'])) { $file_name=$_POST['file_name']; $folder="files/"; $ext=".txt"; $file_name=$folder."".$file_name."".$ext; unlink($file_name); } ?>
In this step we create three isset() function to do three different file operations like create a file, edit existing file and delete a file.
In first isset() function we get file name entered by the user to create file with that name we specify '.txt' file extension you can specify any extension or you can ask from user also then we use fopen
function to create file and then using fclose function to close that file.
See fopen is used for both purposes creating and editing a file.
In third isset() function we get file name to delete entered by the user then we use unlink function to delete that file from folder.
You may also like add,edit and delete rows from table dynamically.
Thats all, this is how to create, edit and delete file using PHP and HTML. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on file operations in php helps you and the steps and method mentioned above are easy to follow and implement.