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

<?php 
$string = $_POST["content"];
$fname=$_FILES["file"]["name"];
if($_POST["show"]){ 
if(isset($_FILES["file"]["name"])){
$fp = fopen($fname, 'r');
while(!feof($fp)){
$file= fgets($fp);
}

}
if($_POST["edit"]){ 
if(isset($_FILES["file"]["name"])){ 
$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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<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;
if(file==""){
alert("Please select File.");
document.form1.file.focus();
return false;
}
return true;
}
</script>

</head>
<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"> 
<tr>
<td><strong>Open File</strong></td>
<td><input type="file" id="file" name="file"></td>
</tr>
<tr>
<td><strong>Content</strong></td>
<td><textarea rows="25" cols="40" name="content"><?=$file?></textarea></td>
</tr>
<tr>
<td> </td><td><input type="submit" name="show" value="Show"/>
<input type="submit" name="edit" value="Edit"/></td><td> </td> 
</tr>
</table>
</form>
</body>
</html>

Ads