php文件下载头 [英] php file download headers
问题描述
这已经让我陷入困境了很多研究之后,我尝试了几种方法。
This has had me stuck for a long time and I have tried several approaches after a lot of research.
这是我当前的下载文件代码: / p>
This is my current code for my download file:
<?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数据库中)。当前的下载链接指向载入上述代码的download.php。此代码允许我下载一个名为download.php的文件,其中不包含任何内容。我从用户个人资料页面调用函数filedownload()。
On another php page I am populating a table of query results, each result gets a corresponding download link. If I mouse over the link I can verify that its download id is correct (in mysql db). The download link currently points to download.php which holds the above code. This code allows me to download a file titled "download.php" which contains no content. I call the function "filedownload()" from a user profile page.
这是我如何显示下载链接:
This is how I am displaying the download link:
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>';
}
以下是调用标题功能的页面中的代码。
Below is code from the page which calls the header function.
<?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();
}
}
?>
推荐答案
您创建了一个函数 ()
但是你永远不会调用它,所以它的主体永远不会被执行。您也不会定义 $ realfile
,您要发送的文件的路径。
You created a function filedownload()
but you never call it, so its body is never executed. You also never define $realfile
, the path to the file you want to send.
调用您的函数: / p>
Call your function:
<?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();
?>
尽管您似乎没有理由在第一个功能中拥有此功能:
Though there doesn't appear to be a reason you have this in a function in the first place:
<?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);
?>
这篇关于php文件下载头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!