0

I'm trying to download .sql.bz2 file using php. But i am unable to do it.

My code is downloading file but file not opening. I want file location to be hidden from user.

I am using following code:

$folderroot = $_SERVER['DOCUMENT_ROOT'];
$fileurl = $folderroot."/dbname.sql.bz2";   
$downloadfilename = generaterandomcharacters(10).".sql.bz2";          
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename=test.sql.bz2');
header('Pragma: no-cache');
readfile($fileurl);
  • is it a duplicate? http://stackoverflow.com/a/13513443/383834 – Stasik Jan 23 '15 at 12:15
  • Your question is not clear. The is file downloading but still want to download it? – DarkBee Jan 23 '15 at 12:15
  • When uploading file via form than file is not located in `DOCUMENT_ROOT` (check via FTP, there is no new files). Check `$_FILES` array for `tmp_name` to get path of file. – Justinas Jan 23 '15 at 12:19
  • File is downloading but its not actual file, its not opening – David Smith Jan 23 '15 at 12:29
  • Open the file in notepad, you will see notices/error or output thas was set before downloading the file. Thats why its corrupt then – DarkBee Jan 23 '15 at 16:16

1 Answers1

0

I just tried your code and it worked for me.

So I'd say that the value your giving to $fileurl is probably incorrect.

Try this :

$folderroot = $_SERVER['DOCUMENT_ROOT'];
$fileurl = $folderroot."/dbname.sql.bz2";   
if ( file_exists($fileurl) ) {
    header("Content-Type: application/octet-stream");
    header('Content-Disposition: attachment; filename=test.sql.bz2');
    header('Pragma: no-cache');
    readfile($fileurl);
}
else
{
 echo 'cannot find file : ' . $fileurl;
}
rtome
  • 1,953
  • 2
  • 22
  • 27
  • Download is working but file download is not correct – David Smith Jan 23 '15 at 12:29
  • what do you mean by "file download is not correct" ? Do you mean that the browser doen't know how to open it and just saves it ? That's is normal with the **Content-Type: application/octet-stream**. Is it something else ? Please clarify. – rtome Jan 23 '15 at 12:37
  • I am getting following error: archive is damaged, if i download file directly its working fine. – David Smith Jan 23 '15 at 12:39
  • try to add header('Content-Transfer-Encoding: binary');, please look into http://stackoverflow.com/a/13513443/383834 – Stasik Jan 23 '15 at 12:40
  • it works ok for me with chrome and firefox on ubuntu. What app are you using to open the file ? Also do try @Stasik 's suggestion, although it works for me without. – rtome Jan 23 '15 at 12:54
  • What app are you using to open it ? How did you create your bz2 archive ? Can you open the file if you recover it via other means that your download page ? – rtome Jan 23 '15 at 12:58
  • This is the url: http://64.64.25.126/facebook/clients/amin/waver/list.php you can try downloading jan 03 backup – David Smith Jan 23 '15 at 13:14
  • I just get a page which says "nofile" for jan 03 and all the other links. – rtome Jan 23 '15 at 13:17
  • i think you tried some other date try January, 03, 2015 – David Smith Jan 23 '15 at 13:20
  • Tried again. Same error. Tried **3rd, January, 2015 Download backup** resulted again in white page saying "nofile". Other date links behave the same way. – rtome Jan 23 '15 at 13:29
  • Yep it works now. I can download the archive and open it. But file extracted is binary. Is it supposed to ? – rtome Jan 23 '15 at 13:44
  • ok it should be sql file its not working on my end what tool you use to open file ? – David Smith Jan 23 '15 at 13:49
  • Just the default archive app in ubuntu 14.04 : Archive Manager – rtome Jan 23 '15 at 13:54
  • The question is how do you generate your archive file ? – rtome Jan 23 '15 at 13:55
  • These files are posted from another server backup generated from standard linux command – David Smith Jan 24 '15 at 15:41
  • ok which command (with options) do you use for the backup and which command (and options) do you use for the transfer ? – rtome Jan 24 '15 at 17:30