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
  • 275
  • 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

6

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
  • 43,532
  • 40
  • 152
  • 238
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
  • 183,406
  • 48
  • 308
  • 385