我有一个PHP脚本来读取线文件N到线(N)。我是不是意味着我将选择从下拉列表中的行和脚本将读取file.I尝试这个和其他一些但不解决我的问题。
的PHP
<?php
if(isset($_POST['submit'])){
$selected_file = $_POST['files'];
$from_line = $_POST['from_line'];
$to_line = $_POST['to_line'];
$file_open = fopen($selected_file,"r+") or die('Fail to open a file');
$file_array = file($selected_file);
$read = fread($file_open,3000);
}
?>
的HTML
<form name="read" method="post" action="">
<table>
<tr><td>Select File</td><td><select name="files" style="width:183px;">
<?php
foreach (scandir('.') as $file){
$extension = pathinfo($file, PATHINFO_EXTENSION);
if($extension == 'txt'){
echo"<option>".$file."</option>";
}}
?>
</select></td>
<tr><td>From</td><td><select name="from_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>To</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>
<td><input type="submit" name="submit" value="Read" /></td></tr>
<tr><td></td></tr>
</table>
</form>
我想要的是
我将从下拉列表中选择值,脚本将在该范围内读取,例如从第4行到第8行
如果我错了,请更正我的脚本...谢谢。
使用具有file
功能的简单逻辑。看下面的解决方案:
if(isset($_POST['submit'])){
$selected_file = $_POST['files'];
$from_line = $_POST['from_line'];
$to_line = $_POST['to_line'];
$file_array = file($selected_file);
for($i = 0; $i < count($file_array); $i++){
if($i >= ($from_line -1) && $i < ($to_line)){
echo $file_array[$i];
echo '<br />';
}
}
}
它将在文件中回显所需的行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句