0

Good Day Everyone Ok, so I have done as much as I understand and need some direction and help. Currently i'm very new to html/php so please bear with me. The plan is to list the text files from a Dir in a dropdown list, this I have done, now I would like to display the text file in the same page in a table upon submit button press. This is what I have so far, any input welcome as I am still learning! The bash script is just a grep function to grab specific lines from the original file and copy it to /tmp. Thanks Again

<html>
<head>
<title>Data Request</title>
</head>
<body>
<h1 align="center">Dispatch Report</h1>
<h2 align="center">Wrong Arrives Report</h2>

<table align="center" border="2">
<tr>
<td>Select Shift<br>
<form name="shiftfrm" id="shiftfrm">
<select name="shiftlist" id="shiftlist">
  <option value="" selected="selected">--------</option>
<?php
$dir = opendir ("/var/www/files/");
    while (false !== ($file = readdir($dir))) {
            if (strpos($file, '.txt',1)) {
                echo '<option value="' . $file . '">' . $file . '</option>';
            }
    }
?>
</select>
<input type="submit" id="submit" value="Submit"/>

<?php
if( ($handle = fopen( '/tmp/sh130418n.txt', 'r' )) !== false )
{
$output = '<table align="center" width="" border="2">';
while( ($data = fgetcsv( $handle )) !== false )
{
    $output .= '<tr>';
    foreach( $data as $value )
    {
        $output .= sprintf( '<td>%s</td>', $value );
    }
fclose( $handle );
$output .= '</table>';
}
echo $output;
?>

</td></tr>
</table>

<?php
$output = exec('/var/www/cgi-bin/manualexceptget.sh');
echo "<pre>$output</pre>";
?>

</body>
</html>
4

2 回答 2

0

假设<form method="post" action="">。增加你的文件阅读代码:

if(!empty($_POST['shiftlist'])) {
    $file = 'files/'.$_POST['shiftlist'];
    if(file_exists($file)) {
        if( ($handle = fopen( $file, 'r' )) !== false )
        {
            $output = '<table align="center" width="" border="2">';
            while( ($data = fgetcsv( $handle )) !== false )
            {
                $output .= '<tr>';
                foreach( $data as $value )
                {
                    $output .= '<td>'.$value.'</td>';
                }
                $output .= '</table>';
            }
            echo $output;
            fclose( $handle );
        }
    }
}

编辑:修复了如下所述的 isset() 问题。更改了一些代码,$handle 在读取完成之前已关闭。

Edit2:我只是把它放在编辑器中,看到许多没有正确放置的 html 标签(例如表单未关闭)。pastebin的工作样本(在 xampp 上测试)

于 2013-05-06T05:59:51.207 回答
0

尝试这样的(.txt)文件..

<html>
<head>
<title>Data Request</title>
</head>
<body>
<h1 align="center">Dispatch Report</h1>
<h2 align="center">Wrong Arrives Report</h2>

<table align="center" border="2">
<tr>
<td>Select Shift<br />
<form name="shiftfrm" id="shiftfrm" method="POST">
<select name="shiftlist" id="shiftlist">
  <option value="" selected="selected">--------</option>
<?php

$dir = opendir("files/");
while (false !== ($file = readdir($dir)))
{
    if (strpos($file, '.txt', 1))
    {
        echo '<option value="' . $file . '">' . $file . '</option>';
    }
}

?>
</select>
<input type="submit" name="submit" id="submit" value="Submit"/>
<br />
<?php

if (isset($_POST['submit']) && isset($_POST['shiftlist']))
{
    if ($handle = file_get_contents('files/' . $_POST['shiftlist']))
    {
        $output = '<table align="center" width="" border="2">';
        $output .= '<tr><td>';
        echo $handle;
        $output .= '</tr></td>';
        $output .= '</table>';
    } else
    {
        echo "No Content";
    }
} else
{
    echo "Please select the file";
}

?>
</td>
</tr>
</table>
</body>
</html>
于 2013-05-06T06:14:20.193 回答