Opening a file in a HTML form


Opening a file in a HTML form

In this example you will learn how to open a file in a HTML form.

In this example you will learn how to open a file in a HTML form.

Opening a file in a HTML form

In this example, you will first see that a form has been created in HTML format and the code of opening a file has been written separately. 

First create a HTML form and call the php action file to open a file in PHP.

Opening a File in PHP

$string = $_POST["content"];
$fp = fopen($fname, 'r');
$file= fgets($fp);

$fp1 = fopen($fname, 'a');
fputs($fp1, $string);

Making a HTML action form

Draw a  table for browsing and browse button.

Draw a table for opening the content. Add show and edit button in it. Let's see the steps:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<head><title>Edit File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
function validate(){ 
var file = document.getElementById("file").value;
alert("Please select File.");
return false;
return true;

<body > 
<form name="form1" onSubmit="return validate();" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">

<table style="border:1px solid #CCCCCC; background-color:#F0F0F0; font-family:verdana; font-size:12px" cellpadding="5" cellspacing="2" width="600px"> 
<td><strong>Open File</strong></td>
<td><input type="file" id="file" name="file"></td>
<td><textarea rows="25" cols="40" name="content"><?=$file?></textarea></td>
<td> </td><td><input type="submit" name="show" value="Show"/>
<input type="submit" name="edit" value="Edit"/></td><td> </td>