1

So far here what i've tried it can download the sql file but it is empty

//test.php

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

Here is what my root folder look like

enter image description here

I want to download the wordpress_db1.sql file when I run the test.php but I always get empty on it. How can I fix this? thanks!

MekeniKine
  • 285
  • 1
  • 6
  • 13
  • possible duplicate of [Export MySQL database using PHP only](http://stackoverflow.com/questions/22195493/export-mysql-database-using-php-only) – T.Todua Nov 27 '14 at 20:57

2 Answers2

7

Below code will do the trick for you.

<?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);
?>

What have you gone wrong is readfile($file_url);. Setting headers will not get the job done. you have use readfile($file_url);

Techie
  • 44,706
  • 42
  • 157
  • 243
5

Setting the headers doesn't read the file. You can name the file anything you want in the attachment. You have to actually emit the file:

readfile('wordpress_db1.sql');
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405