File Editor Using PHP

In this tutorial we will show you how to open, edit and delete file using PHP and HTML. File handling is an important part of any web application. You often need to open and process a file for different tasks. PHP Manipulating Files. PHP has several functions for creating, reading, uploading, and editing files

This chapter will explain following functions related to file

1. Choose which file to open
2. Read the contents of a file
3. Save content to file


  • List files and directories inside the specified path
define('EDITABLE_FORMATS', 'txt,php,htm,html,js,css,tpl,xml,md');
define('SHOW_HIDDEN_FILES', false);

function files($dir) {
	$formats = explode(',', EDITABLE_FORMATS);
	$data = '<ul class="files list-group" style="display:' . $display . '">';
	$files = array_slice(scandir($dir), 2);
	foreach ($files as $key => $file) {
		if ($dir . DIRECTORY_SEPARATOR . $file == __FILE__ || (SHOW_HIDDEN_FILES === false && substr($file, 0, 1) === '.'))
		$writable = is_writable($dir . DIRECTORY_SEPARATOR . $file) ? 'writable' : 'non-writable';
		if (is_dir($dir . DIRECTORY_SEPARATOR . $file))
			$data .= '<li class="dir ' . $writable . ' list-group-item"><a href="javascript:void(0);" onclick="return expandDir(this);" data-dir="' . str_replace(__DIR__ . '/', '', $dir . DIRECTORY_SEPARATOR . $file) . '">' . $file . '</a>' . files($dir . DIRECTORY_SEPARATOR . $file, 'none') . '</li>';
		else {
			$is_editable = strpos($file, '.') === false || in_array(substr($file, strrpos($file, '.') + 1), $formats);
			$data .= '<li class="file ' . $writable . ' ' . ($is_editable ? 'editable' : null) . ' list-group-item">';
			if ($is_editable === true)
				$data .= '<a href="#' . $file . '" onclick="return openFile(this);" data-file="' . str_replace(__DIR__ . '/', '', $dir . DIRECTORY_SEPARATOR . $file) . '">';
			$data .= $file;
			if ($is_editable)
				$data .= '</a>';
			if ($writable === 'writable')
				$data .= ' <a href="javascript:void(0);" class="btn btn-sm btn-danger visible-on-hover" onclick="return deleteFile(this);">Delete</a>';
			$data .= '</li>';
	$data .= '</ul>';
	return $data;

Displaying files and directory present inside any directory by using scandir function in PHP, then choose which file to open.


  • Read the contents of a file and display it in a textarea.
<?php echo file_get_contents(__DIR__ . DIRECTORY_SEPARATOR .  $_GET['file']); ?>

In PHP, file_get_contents() function is one of the file handling functions available in this scripting language. This function reads entire file source or part of it and returns as a PHP string data. Because it will use memory mapping techniques, if this is supported by the server, to enhance performance. This function is binary-safe (meaning that both binary data, like images, and character data can be written with this function).


  • Save content to file

You create a HTML form to edit the textfile’s content. In case it get’s submitted, you update the file.

if (isset($_POST['edit-content'])) {
    $file = __DIR__ . DIRECTORY_SEPARATOR . $_GET['file'];
        file_put_contents($file, $_POST['edit-content']);
        $updated = true;
        //echo 'Is not writable file';
        $updated = false;

The file_put_contents() writes a string to a file. Specifies the file to write to. If the file does not exist, this function will create one. Always use full paths and make sure the directory is writable.



Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.