1

我正在返回一个带有相应“下载”链接的表,这些链接由我的数据库中的 id 分配。用户单击“下载”并收到通知,他们将开始下载文件。现在我可以点击“下载”,我下载的文件不是所需的文件,它是 php 页面的回声。

这是当前代码:

配置文件.php

   <?php if (isset($_POST['query']))
   {
   require_once ('../mysqli_connect.php'); //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




                    // Make sure an ID was passed DOWNLOAD HANDLER *******
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
        $downloadq = "
            SELECT `file_type`, `size`, `title`, 'content', 'upload_id'
            FROM `upload`
            WHERE `upload_id` =".$id;
        $result = mysqli_query ($dbc, $downloadq); // Run the query


        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);

                $place = './uploads/'.$_SESSION_['email'].'/';
                $thefile = $place.$row['title'];

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


                // Print headers
              // header("Content-Type: application/msword");
              // header("Content-Length: ". $row['size']);
              // header("Content-Disposition: attachment; filename=".$row['title']);
              // header("Content-Transfer-Encoding: binary");
              // readfile($thefile);



                // Print data
                //echo (stripslashes($row['content']));



                exit;


            }
            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);



    }

                    ?>

下载.php

<?PHP

function filedownload()
{
               header("Content-Type: application/msword");
               header("Content-Length: ". $row['size']);
               header("Content-Disposition: attachment; filename=".$row['title']);
               header("Content-Transfer-Encoding: binary");
               readfile($thefile, 1);

}
               ?>

表中的“下载”链接与数据库中文件的 ID 正确对应。单击“下载”会下载一个名为 profile.php 的文件,其中包含我的 php 和 html 标记。在此先感谢,这让我很难过。

4

1 回答 1

1

试着放

global $row, $thefile;

作为 filedownload() 函数中的第一行。我注意到您正在尝试使用此函数外部的变量。

于 2012-08-12T20:58:28.503 回答