gpt4 book ai didi

php - 如何使用php下载pdf文件

转载 作者:搜寻专家 更新时间:2023-10-30 20:26:45 24 4
gpt4 key购买 nike

我想下载 pdf 文件或任何其他文件,但我的主要动机是下载 pdf 文件。当用户提交查询时,我们可以在浏览器上看到 pdf 文件而不是下载吗?

我的上传代码工作正常`

<!DOCTYPE html>
<html>
<body>

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

</body>
</html>

<?php
$uploadDir = 'C:\wamp\www\images\passport images';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";

$conn = mysqli_connect($servername, $username, $password, $dbname);


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysqli_query($conn,$query) or die('Error, query failed : ' . mysql_error());



echo "<br>Files uploaded<br>";

}
?>

**现在下载代码有问题**。实际上我希望用户在文本框 acc 中提交查询。该文本及其扫描的图像所以以 pdf 格式显示给他

<?php
if(isset($_GET['id']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";

$conn = mysqli_connect($servername, $username, $password, $dbname);

$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '1'";
$result = mysqli_query($conn,$query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysqli_fetch_array($result);

header("Content-Disposition: attachment; filename='".$name."'");
header("Content-length: $size");
header("Content-type: $type");

echo "<a href=" . $uploadDir .($row['userfile']) . ">
{$row['userfile']}</a>";

exit;
}

?>

`

最佳答案

这是我的下载功能,您可以使用它来下载任何文件。

function dawnload_file($path) {
if (file_exists($path) && is_file($path)) {
// file exist
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
set_time_limit(0);
@readfile($path);//"@" is an error control operator to suppress errors
} else {
// file doesn't exist
die('Error: The file ' . basename($path) . ' does not exist!');
}
}

这是我在浏览器中在线打开 pdf 文件的功能。

function read_pdf_online($path){
if (file_exists($path) && is_file($path)){
// file exist
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path));
@readfile( $path );

else {
// file doesn't exist
die('File Not Found: ' . basename($path));
}
}

关于php - 如何使用php下载pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com