您好我需要使用php从mysql db下载pdf文件。它适用于localhost,但是当我在服务器上托管它时,它不会工作,说"无法加载pdf"。我真的不知道我哪里出错了。任何人都可以建议我。
<?php
include 'connection.php';
$sql=mysqli_query($connection,"Select name,content from pdf where id = (select max(id) from pdf)");
$result=mysqli_fetch_assoc($sql);
$filename=$result['name'];// echo $filename;
$filecontent=$result['content'];
header('Content-type: application/pdf');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Disposition: inline; filename="'.$filename.'"');
readfile($filecontent);
mysqli_close($connection);
?>
上传代码
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td>please select a file</td></tr>
<tr>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="16000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['upload'])&&$_FILES['userfile']['size']>0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName); echo "<br>".$fileName."<br>";
}
if($db)
{
$query = mysqli_query($connection,"INSERT INTO pdf (name, size, type, content) VALUES ('$fileName', '$fileSize', '$fileType', '$content')") or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
else
{
echo "file upload failed";
}
}
?>
答案 0 :(得分:1)
因为您使用的是varchar(10000)
,所以pdf中的大量数据都会被截断。您必须在数据库中使用binary
字段,这将保留文件内容中的所有特殊字符。
答案 1 :(得分:0)
<?php
include 'connection.php';
$sql=mysqli_query($connection,"Select name,content from pdf where id = (select max(id) from pdf)");
$result=mysqli_fetch_assoc($sql);
$filename=$result['name'];// echo $filename;
$filecontent=$result['content'];
var_dump($filecontent, file_exists($filecontent));
mysqli_close($connection);
$ filecontent的var_dump值,看看它是否是文件名。
如果是文件名,请检查您的localhost中是否存在该文件,但托管服务器中是否存在。