在另一个 php 中调用 download.php

我有一个包含在表单操作中的按钮,单击该按钮会调用下面的 download.php 并下载已经存在的文件。


在 results.php 中形成动作


<form method="post" action="download.php">

        <button type="submit" class="btn btn-dark btn-block" data-toggle="dwn-one" title="Click to download"> Download </button>

     </form>

下载.php


<?php

        header("Content-disposition: attachment; filename=file1.txt");

        header("Content-type: application/txt");

        readfile("file1.txt");

        header("results.php");

?>

上述功能运行良好,但现在我有很多文件要下载,所以我想避免多个下载按钮,而是提供一个选择功能,用户可以选择文件并单击下载按钮。到目前为止,我正在执行如下操作,但无法弄清楚如何调用 download.php。


选择代码


<form method = "post" action="">

   <select class="form-control" name="downloaditem">

        <option value="1">Select a file to download</option>

        <option value="2">File one</option>

        <option value="3">File two</option>

        <option value="4">File three</option>

        <option value="5">File four</option>

      </select>

 <button type="submit" class="btn btn-info" name="files">Download</button>

</form>

isset 函数


if(isset($_POST['files']))

{

        $filesel = $_POST['downloaditem'];

        if($filesel == "2")

        {

      //call to download.php

        }

}

请帮忙。


BIG阳
浏览 118回答 1
1回答

慕的地6264312

只需将表单发送到 download.php 脚本。<form method = "post" action="<url-to>download.php">&nbsp; &nbsp;<select class="form-control" name="downloaditem">&nbsp; &nbsp; &nbsp; &nbsp; <!-- It's not necessary to make the first option available for select -->&nbsp; &nbsp; &nbsp; &nbsp; <option value="0" disabled>Select a file to download</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="1">File one</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="2">File two</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="3">File three</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="4">File four</option>&nbsp; &nbsp; &nbsp; </select>&nbsp;<button type="submit" class="btn btn-info" name="files">Download</button></form>下载.php// Get the file name and path to the file based on the given id// or return nullfunction getFile($id) {&nbsp; &nbsp; switch((int)$_POST['downloaditem']) {&nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ['file1', 'path/to/file1.txt'];&nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ['file1', 'path/to/file2.txt'];&nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ['file1', 'path/to/file3.txt'];&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}if(isset($_POST['files'])) {&nbsp; &nbsp; $file = getFile($_POST['downloaditem']);&nbsp; &nbsp; if (null !== $file} {&nbsp; &nbsp; &nbsp; &nbsp; header('Content-Disposition: attachment; filename=' . $file[0]. '.txt');&nbsp; &nbsp; &nbsp; &nbsp; header('Content-Type: application/txt');&nbsp; &nbsp; &nbsp; &nbsp; readfile($file[2]);&nbsp; &nbsp; &nbsp; &nbsp; header('results.php');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // If function returns null show a HTTP 404 error&nbsp; &nbsp; &nbsp; &nbsp; header('Content-Type: text/plain');&nbsp; &nbsp; &nbsp; &nbsp; header('HTTP/1.1 404 Not Found');&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP