PHP脚本,用于动态生成当前目录中存在的PDF文件的链接

问题描述 投票:0回答:2

我有一个PHP代码读取TEXT文件,并根据用户请求搜索字符串。系统在表中显示结果,该结果包含文件名和存在的行号。

每个TEXT文件在同一文件夹中将其另一个副本作为PDF文件。

我需要将TEXT文件作为PDF文件的超链接。

到目前为止,我已经成功制作了TEXT文件的超链接

我知道这条线必须有扩展但我不知道如何添加:

$filenameHtml .= "<th><a href ='".$filename."' target='_blank'>$filename</a></th>";

截图:

the displayed result

code:

 <?php

$result = [];
if(isset($_POST["search"]))
{
    $search =$_POST['name'];
    echo "the word  $search exist: <br><br>";
    foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
    {
        $line = 1;
        $temp   = [];
        $myFileLink = fopen($txts, 'r');
        while(!feof($myFileLink))
        {
            $myFileContents = fgets($myFileLink);
            if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
            {

                $temp['filename'] = basename ($txts);
                foreach($matches[1] as $match)
                {
                    $temp['lines'][] = $line;
                }
            }
            ++$line;
        }
        fclose($myFileLink);

        $result[] = $temp;
    }

    //display the table
    echo '<table border=2>';

    $filenameHtml    = '<tr>';
    $lineNumberHtml    = '<tr>';
    foreach ($result as $item){
     $filename = isset($item['filename']) ? $item['filename'] : '';
     $lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
     //$filenameHtml .= "<th>$filename</th>";
     $filenameHtml .= "<th><a href ='".$filename."' target='_blank'>$filename</a></th>";
     $lineNumberHtml .= "<td>$lines</td>";
    }
    $filenameHtml    .= '</tr>';
    $lineNumberHtml  .= '</tr>';

    echo $filenameHtml.$lineNumberHtml;
    echo '</table>';
}
?>

<html>
    <head>
    </head>
    <meta http-equiv="Content-Language" content="ar-sa">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
          #form {
        background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
        margin: auto;
        width: 200px;
        height: 200px;
        position: absolute;


        font-family: Tahoma, Geneva, sans-serif;
        font-size: 14px;
        font-style: italic;
        line-height: 24px;
        font-weight: bold;
        color: #09C;
        text-decoration: none;
        border-radius: 10px;
        padding: 10px;
        border: 1px solid #999;
        border: inset 1px solid #333;
        -webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        -moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
        box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
      }

    </style
    <body>
    <div id = "form">
        <form action="index.php" method="post">
          <h1 align =center > Search Form </h1>
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p align =center ><input type ="Submit" name ="search" value= "Search" /></p>
        </form>
    </div>
    </body>
</html>
php html hyperlink
2个回答
0
投票

您可以使用str_replace将.file变量中的扩展名从.txt更改为.pdf。

$new_filename = str_replace('.txt', '.pdf', $filename);

0
投票

或者以更好的方式,您可以读取整个文本文件并将其存储在变量中,并使用PDF库(如mpdf mpdf)生成pdf

或者,如果您以html格式显示数据,则使用printjs在运行时创建pdf,这将减少服务器上的负载并节省空间。

© www.soinside.com 2019 - 2024. All rights reserved.