i have a txt file containing 20 lines i want to replace line # 4 with a new text from an input ...i have tried this and some others but not solving my problem. PHP Code
<?php
if(isset($_POST['submit'])){
$selected_file = $_POST['files'];
$line = $_POST['to_line'];
$text = $_POST['text'];
//opn file
$file_open = fopen($selected_file,"r+") or die('Fail to open a file');
//fwrite($file_open,$text);
$file_array = file($selected_file);
$file_array[$line] = $text;
$file_array = implode($file_array);
file_put_contents($selected_file,$file_array);
?>
HTML
<form name="write" method="post" action="">
<table>
<tr><td>Select File</td><td><select name="files" style="width:183px;">
<?php
foreach ($a = scandir('.') as $file){
$extension = pathinfo($file, PATHINFO_EXTENSION);
if($extension == 'txt' || $extension == 'doc'){
echo"<option>".$file."</option>";
}}
?>
></select></td></tr>
<tr><td>Line</td><td><select name="to_line" style="width:183px">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
</td></tr>
<tr><td>Write Text</td><td><textarea name="text" style=" height:120px" ></textarea></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Save" /></td></tr>
</table>
</form>
what i want i want to select a value from drop-down list (line #) and that whole line will be replace with the new text.