Php 下载文件或打开(如果文件类型为PDF)
我的代码就在这里:Php 下载文件或打开(如果文件类型为PDF),php,pdf,file-io,download,Php,Pdf,File Io,Download,我的代码就在这里: if(isset($_POST['file_id'])){ $file_id = $_POST['file_id']; $query = $mysqli->query("SELECT `IdDocumento`,`nome_ficheiro`,`caminho_ficheiro` FROM `documento` WHERE `IdDocumento` = $file_id");
if(isset($_POST['file_id'])){
$file_id = $_POST['file_id'];
$query = $mysqli->query("SELECT `IdDocumento`,`nome_ficheiro`,`caminho_ficheiro`
FROM `documento`
WHERE `IdDocumento` = $file_id");
$result = $query->fetch_object();
$caminho = $result->caminho_ficheiro;
$nome = $result->nome_ficheiro;
header('Content-Disposition: attachment; filename="'.$nome.'"');
readfile($caminho);
exit();
}
从一个页面下载文件。它适合下载。但我想在代码中添加一个if。。
比如:
if (file is a PDF) {
header('Content-type: application/pdf');
}else{
header('Content-Disposition: attachment; filename="'.$nome.'"');
readfile($caminho);
exit();
}
因此,如果文件是PDF文件,它会打开文件而不是下载文件。
我该怎么做?我搜索了,但找不到如何检查文件类型
编辑(正确的解决方案):
<代码> >(文件是PDF){条件可以以多种方式实现;如果文件有“<代码> PDF扩展名:,则更容易考虑文件是PDF”。
if (strtolower(pathinfo($nome, PATHINFO_EXTENSION)) == 'pdf') {
顺便说一句,这段代码中有一个。使用。这段代码中有一个。使用。谢谢,它工作了:)你能告诉我我是否正确使用了准备好的语句吗?我用准备好的语句编辑了问题。
if (strtolower(pathinfo($nome, PATHINFO_EXTENSION)) == 'pdf') {