Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单击“下载文件”按钮后是否不返回任何内容?_Php_Sql - Fatal编程技术网

Php 单击“下载文件”按钮后是否不返回任何内容?

Php 单击“下载文件”按钮后是否不返回任何内容?,php,sql,Php,Sql,我已经创建了一个上载页面来上载文件,然后在单击上载按钮后更改其名称 这一切都发生在后端“管理站点” 在前端,我提供了以下代码: <?php foreach ($files as $file): ?> <div class='form-group'> <p><a href="contact.php?fn=<?php echo $file['file_id'] ?>">Click here to downloa

我已经创建了一个上载页面来上载文件,然后在单击上载按钮后更改其名称 这一切都发生在后端“管理站点” 在前端,我提供了以下代码:

 <?php foreach ($files as $file): ?>
 <div class='form-group'>
   <p><a href="contact.php?fn=<?php echo $file['file_id'] ?>">Click here to download the file</a></p>
 </div>
 <?php endforeach;?>
但是什么都没发生它只是刷新页面而没有下载任何东西

我希望有人能帮我解决这个问题

//fetch all recorders in the table
$sql = "SELECT * FROM policy";
$result = mysqli_query($conn, $sql);

$files = mysqli_fetch_all($result, MYSQLI_ASSOC);

//get the id number from the request 

if (isset($_GET['fn'])) {
    $id = $_GET['fn'];
// fetch file to download from database
$sql = "SELECT * FROM policy WHERE file_id=?";
$stmt= mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt,"i",$id);
mysqli_stmt_execute($stmt);
$result=mysqli_stmt_get_result($stmt);
$file=mysqli_fetch_assoc($result);

/* modify the file name by adding extension because the file when uploaded in database it 
uploaded without extension name so it can match the recorders in uploading folder  */
 $filename=$file['name'].'.pdf';
$filepath = 'uploads/' .$filename;

if (file_exists($filepath)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($filepath));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    
    header('Content-Length: ' . filesize('uploads/' . $filename));
   
    readfile('uploads/' . $filename);

    // Now update downloads count
    $newCount = $file['downloads'] + 1;
    $updateQuery = "UPDATE policy SET downloads=$newCount WHERE file_id=$id";
    mysqli_query($conn, $updateQuery);
    exit;
}

}