【问题标题】:php file download headersphp文件下载头文件
【发布时间】:2012-08-29 01:12:50
【问题描述】:

这让我卡了很长时间,经过大量研究,我尝试了几种方法。

这是我当前下载文件的代码:

<?php
  require_once ('./newpub_profile.php');
  $thetitle = $row['title'];
  $thesize = $row['size'];

 function filedownload() { 
    GLOBAL $thesize, $thetitle;
    header("Content-Type: application/msword");
    header("Content-Disposition: attachment; filename=".$thetitle);
    header("Content-Length: ".$thesize);
    header("Content-Transfer-Encoding: binary");
    readfile($realfile);
 }
?>

在另一个 php 页面上,我正在填充一个查询结果表,每个结果都有一个相应的下载链接。如果我将鼠标悬停在链接上,我可以验证其下载 ID 是否正确(在 mysql db 中)。下载链接当前指向包含上述代码的 download.php。此代码允许我下载一个标题为“download.php”的文件,其中不包含任何内容。我从用户个人资料页面调用函数“filedownload()”。

这是我显示下载链接的方式:

while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
        {
            echo '<tr><td align="left">' .
            $row['title'] . '</td><td align="left">'
            . $row['genre'] . '</td><td align="left">'
            . $row['length'] . '</td><td align="left">'
            . $row['created'] . '</td><td align="left">'
            //. $row['views'] . '</td><td align="left">'
            . "<a href='download.php?id={$row['upload_id']}'>Download</a></td>" . '</td></tr>';
        }

下面是调用 header 函数的页面代码。

    <?php if (isset($_POST['query']))
   {
   require_once (connectionToDB); //Connect to the db

    // Make the query
   $genre = $_POST['select_genre'];
   $length = $_POST['select_length'];

    $upviews = "UPDATE upload
                SET views = views + 1
                WHERE genre = '$genre' AND length = '$length'";
    $runviewupdate = mysqli_query ($dbc, $upviews);


    $q = "SELECT upload_id, title, genre, length, created
        FROM upload
        WHERE genre = '$genre' AND length = '$length'
        ORDER BY created DESC, title DESC";


    $r = mysqli_query ($dbc, $q); // Run the query

    if($r)
    {
        // If it ran okay, display the records
        echo '<table align="center"
            cellspacing="3" cellpadding="3"
            width="75%">
           <tr><td align="left"><b>Title</b></td>
           <td align="left"><b>Genre</b></td>
           <td align="left"><b>Pages</b></td>
           <td align="left"><b>Submitted</b></td>
           <td align="left"><b>Download</b></td>';

        // Fetch and print all the records:

        while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
        {
            echo '<tr><td align="left">' .
            $row['title'] . '</td><td align="left">'
            . $row['genre'] . '</td><td align="left">'
            . $row['length'] . '</td><td align="left">'
            . $row['created'] . '</td><td align="left">'
            //. $row['views'] . '</td><td align="left">'
            . "<a href='newpub_profile.php?id={$row['upload_id']}'>Download</a></td>" . '</td></tr>';
        }
        echo '</table>'; // Close the table

        mysqli_free_result ($r); // Free up the resources
    }
    else // If it did not run okay
    {
        // Public Message:

        echo '<p class="error">Your submissions could not be retrieved.  We
            apologize for any inconvenience.</p>';

        // Debugging message:

        echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

    } // End of if ($r) IF.



}

//END DOWNLOAD HANDLER ******************************************************


    mysqli_close($dbc); // Close the database connection



if(isset($_GET['id'])) {
// Get the ID
    $id = intval($_GET['id']); //var_dump($id);

    require_once ('../mysqli_connect.php'); //Connect to the db

// Fetch the file information

    if($stmt = $mysqli -> prepare("SELECT `file_type`, `size`, `title`, 'content', 'upload_id'
            FROM `upload`
            WHERE `upload_id` =?")) {

      /* Bind parameters
         s - string, b - boolean, i - int, etc */
      $stmt -> bind_param("sissi", $file_type, $size, $title, $content, $upload_id);

      /* Execute it */
      $stmt -> execute();   /*FINISH PREPARED STATEMENTS*/

      /* Bind results */
      $stmt -> bind_result($result);

      /* Fetch the value */
      $stmt -> fetch();

      //echo $user . "'s level of priviledges is " . $result;

      /* Close statement */
      //$stmt -> close();


        if($result) {
            // Make sure the result is valid
            if (mysqli_num_rows($result) > 0) {
            // Get the row
                $row = mysqli_fetch_assoc($result);
                //var_dump($row);
                //$data = $row;

                $place = './uploads/'.$_SESSION_['email'].'/';
                $thefile = $place.$row['title'];
                $realfile = $thefile;
                $thetitle = $row['title'];
                $thesize = $row['size'];

               require_once('./download.php');
               filedownload();






            }
            else {
                echo 'Error! No such ID.';
            }

            // Free the mysqli resources
            mysqli_free_result($result);
        }
        else {
            echo "Error! Query failed: <pre>{$dbc->error}</pre>";
        }
        mysqli_close($dbc);
        $stmt -> close();
            }




    }


                    ?>

【问题讨论】:

  • 下载链接不是从我的文件系统下载文件内容。相反,我的浏览器正在下载一个名为“download.php”的文件,并且是一个没有内容的空白页面。
  • 您使用的是什么浏览器和版本?您是否在其他浏览器中尝试过?不同的浏览器需要不同的标头。
  • 他的代码只定义了一个函数……当然没有输出。不要再问他关于他的浏览器的事情了。
  • @DanGrossman 我会假设他的页面有更多的代码。这是一个合乎逻辑的问题。
  • 我一直在使用谷歌浏览器。此外,我的页面确实有更多代码。正在从用户的“个人资料”页面调用该函数

标签: php header download readfile


【解决方案1】:

你创建了一个函数filedownload(),但你从未调用它,所以它的主体永远不会被执行。您也永远不会定义$realfile,即您要发送的文件的路径。

调用你的函数:

<?php
  require_once ('./newpub_profile.php');
  $thetitle = $row['title'];
  $thesize = $row['size'];

 function filedownload() { 
    GLOBAL $thesize, $thetitle;
    header("Content-Type: application/msword");
    header("Content-Disposition: attachment; filename=".$thetitle);
    header("Content-Length: ".$thesize);
    header("Content-Transfer-Encoding: binary");
    readfile($realfile);
 }

 filedownload();
?>

虽然似乎没有理由首先在函数中使用它:

<?php
  require_once ('./newpub_profile.php');
  $thetitle = $row['title'];
  $thesize = $row['size'];

  header("Content-Type: application/msword");
  header("Content-Disposition: attachment; filename=".$thetitle);
  header("Content-Length: ".$thesize);
  header("Content-Transfer-Encoding: binary");
  readfile($realfile);
?>

【讨论】:

  • 我在另一个页面(用户个人资料页面)中调用我的函数。正如我在上面的代码中更新的那样,用户个人资料页面加载了一个带有链接的表格。我想要链接来获取我的 download.php 代码并将文件提供给用户。
  • 好吧,直接未解决的问题仍然是缺少要提供的文件$realfile。您告诉它以未定义的变量发送文件。即使您在另一个页面中定义了$realfile,它也不在函数的范围内。
  • 我更新了问题陈述以合并调用页面。提前感谢您的帮助。
  • $realfile 添加到函数中的全局列表中。
  • 我目前有:全球 $thesize、$thefile、$realfile。单击下载链接可以让我保持在我想要的呼叫(配置文件)页面上,但没有提供任何文件。此外,似乎这段代码之后的所有内容都没有执行。页脚、侧边栏等未加载。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多