PHP:如何使浏览器在单击时下载文件 [英] PHP: How to make browser to download file on click

查看:41
本文介绍了PHP:如何使浏览器在单击时下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP初学者.文件上传成功,但是我的浏览器未下载文件,而是读取了文件.所以我引用了其他线程,发现下面的代码不起作用.当我单击超链接下载时,我想下载文件.从MySQL数据库中选择了路径.

PHP Beginner. File uploading is successful but my browser doesn't download the files, instead it reads the file. So i referred other threads and found below code which is not working. I want to download files when i click on the hyperlink download. Selected the path from MySQL database.

$rows = mysqli_num_rows($result);

                if($rows>0)
                {

                    while($row = mysqli_fetch_assoc($result))
                    {   
                        ?>
                        <div> <?php echo $row['Object_Name'];?> 
                        <a href="<?php 
                        $file_url = $row['Object_Path'];
                        header('Content-Type: application/octet-stream');
                        header("Content-disposition: attachment; filename=\"".$row['Object_Name']. "\""); 
                        readfile($file_url);
                        ?>">Download</a><br>
                        </div>  
                        <?php
                    }

                }

推荐答案

在名为download.php的页面中,具有以下代码:

In a paged called download.php, have the following code:

<?php

$filename = 'file.pdf';//this should be the name of the file you want to download 
header('Pragma: public');
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-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));

readfile($filename);

exit;
?>

您的主页上应该有一个指向下载页面的链接,如下所示:

Your main page should then have a link to the download page like this:

<a href="download.php">DOWNLOAD</a>

让我知道这是否对您有用.

Let me know if that works for you.

我的上一个示例用于下载pdf文件.如果您要下载其他类型的文件,则必须稍微修改几行.建议您先尝试使用先前的代码下载pdf文件,然后再对其他文件进行测试.

要从数据库检索路径,可以使用MySQL(PDO).

To retrieve the path from the database, you can use MySQL (PDO).

$sqlStatement = "SELECT path FROM my_table WHERE some_id = ".$something;
/*if you are retrieving the path from the database, 
you probably have a lot of different paths available 
there, so only you know the criteria which will decide
which of the many paths it is that you choose to extract*/

$sqlPrepared = $connection->prepare($sqlStatement);
$sqlPrepared->execute();

$row_info = fetch($sqlPrepared);

$filename = $row_info['path'];// this would be the $filename = 'file.pdf' 
//that was in the example above

如果不确定如何连接到数据库,在线上有很多文章介绍了相对简单的MySQL.

If you are not sure how to connect to the database, there are a lot of articles online explaining MySQL that is relatively straightforward.

我希望对您有所帮助:)

I hope that helped :)

这篇关于PHP:如何使浏览器在单击时下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆