使用PHP自动下载wwwroot中的文件(.sql)

Mek*_*ine 1 php mysql

到目前为止,我尝试过它可以下载sql文件,但它是空的

//test.php

 <?php
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=wordpress_db1.sql');
?>
Run Code Online (Sandbox Code Playgroud)

这是我的根文件夹的样子

在此输入图像描述

我想在运行test.php时下载wordpress_db1.sql文件,但我总是把它弄空.我怎样才能解决这个问题?谢谢!

Exp*_*lls 5

设置标题不会读取文件.您可以在附件中将文件命名为任何名称.你必须实际发出文件:

readfile('wordpress_db1.sql');
Run Code Online (Sandbox Code Playgroud)


Tec*_*hie 5

下面的代码将为您解决问题.

<?php 
$file_name = 'file.sql';
$file_url = 'http://www.example.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);
?>
Run Code Online (Sandbox Code Playgroud)

你有什么出了问题readfile($file_url);.设置标题将无法完成工作.你有用 readfile($file_url);