Why sql dump create blank file

hi

i am using xampp on windows

i am using below code to create mysql dump

<?php
$DBUSER="root";
$DBPASSWD="";
$DATABASE="autosug";

$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$mime = "application/x-gzip";

header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";   

passthru( $cmd );

exit(0);
?>

This above code create a downloadable sql file but its blank.

why is it creating a blank sql file ??

vineet

Does the mysqldump command line produce the expected result if you execute it from the command line?

Hi droopsnoot

i tried below on cmd

mysqldump -u root autosug

i see few lines of code on command window and in the end it says

dump completed on 2016-7-14

But i cant find any file ??

i think i need to specify filename and location of where to save sql dump file.

But i dont know where do i specify file name and location of “where to save sql file” in the command line ??

can you tell me ??

vineet

Well, according to the manual page for mysqldump you can specify the filename using the --filename option, but the main reason for my asking was to narrow down where the error is. If the few lines of code on the command window is the correct output, could it be the gzip part of the command? For example, do you have gzip installed on your Windows PC? If you type gzip at a windows command line, what happens?

The code you posted won’t output the sql dump to a file - it displays it on the console. Your php code, in the passthru() function, then passes that binary data out to the browser which, combined with the headers you’ve sent, is what I thought you wanted. Saving it to a temporary file might be another way around it, then redirect the user or send the file to the browser, but it seems to add a step for no good reason.

Use option --result-file for mysqldump. I would use separate commands to do this:

  1. mysqldump to a file using exec()
  2. gzip the file using exec()
  3. delete the uncompressed file
  4. send the gzipped file to the browser using readfile()
  5. delete the file

To me this would seem more robust than using passthru.

hi droopsnoot

if i type

c:\>gzip

it says
gzip is not recognized as internal or external command.
operable program or batch file

But the above code creates a zip folder and inside the zip folder there generates a blank sql file.

so command line says its not recognized.
but php creates a zip folder.

what to do now ??

vineet

Hi droopsnoot

i just downloaded gzip application exe from
http://www.gzip.org/#exe

then i copy pasted it in program files

now when i run

c:\program files\gzip>gzip -h

it says

gzip 1.2.4 win32
and then multiple lines showing different options of gzip usage

so now how to create a gzip dump

vineet

Getting that to work on the windows terminal is going to be a very painful experience. That command doesn’t work on WIndows at all. If your production machine is Linux based you should be running a Linux vm. You’re doing everyone a very big disservice developing for Xamp instead of your target environment which I assume isn’t a windows server. Xamp is really only adequate for basic development. Anything beyond that like executing Linux commands it isn’t useful for which is why you should be running a Linux vm instead. Anything you do to make that work with Xamp is going be specific to your own environment.

hi oddz

can i download the unzip version of sql file that is being created with exec command.

can it be made downloadable.

i am talking about the static or unzip versioned file ??

vineet

In general any Windows shell scripting will not work on a Linux server. Just as any Linux shell scripting won’t work on Windows. Windows and Linux are two completely different environments. I don’t know the answer to your question because I avoid those stupid problems by just using a vm. Xamp is garbage for anything beyond basic php programming.

<?php
$DBUSER="root";
$DBPASSWD="";
$DATABASE="autosug";
$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > $filename";
passthru( $cmd );
exit(0);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.