我想下载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>";
}
?>
**Now problem in download code**. Actually I want user submit a query in text box acc. to that text as well as it's image which is scanned So in pdf format shows to him
<?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;
}
?>
`
答案 0 :(得分:1)
这是我可以用来下载任何文件的下载功能。
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));
}
}
答案 1 :(得分:0)
您的标题会告诉浏览器文件数据将会跟随:
header("Content-Disposition: attachment; filename='".$name."'");
[…]
但是您只需输出一个链接而不是文件数据:
echo "<a href=" . $uploadDir .($row['userfile']) . ">{$row['userfile']}</a>";
这将导致错误。您有两种方法可以解决它。输出链接时不会误导标题。当用户点击链接服务器时,会将文件发送到浏览器。如果服务器知道mime类型,它将为您设置标题。
或者在标题之后发送文件数据而不是链接(您可以使用readfile
命令读取和输出文件数据):
header("Content-Disposition: attachment; filename='".$name."'");
header("Content-length: $size");
header("Content-type: $type");
readfile($uploadDir . $row['userfile']);