亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在另一個 php 中調用 download.php

在另一個 php 中調用 download.php

PHP
BIG陽 2022-10-09 20:15:14
我有一個包含在表單操作中的按鈕,單擊該按鈕會調用下面的 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        }}請幫忙。
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

只需將表單發送到 download.php 腳本。


<form method = "post" action="<url-to>download.php">

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

        <!-- It's not necessary to make the first option available for select -->

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

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

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

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

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

      </select>

 <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 null

function getFile($id) {

    switch((int)$_POST['downloaditem']) {

        case 1:

            return ['file1', 'path/to/file1.txt'];

        case 2:

            return ['file1', 'path/to/file2.txt'];

        case 3:

            return ['file1', 'path/to/file3.txt'];

        default:

            return null;

    }

}


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

    $file = getFile($_POST['downloaditem']);

    if (null !== $file} {

        header('Content-Disposition: attachment; filename=' . $file[0]. '.txt');

        header('Content-Type: application/txt');

        readfile($file[2]);

        header('results.php');

    } else {

        // If function returns null show a HTTP 404 error

        header('Content-Type: text/plain');

        header('HTTP/1.1 404 Not Found');

    }

}


查看完整回答
反對 回復 2022-10-09
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號