Forum Moderators: coopster

Message Too Old, No Replies

Script download GZ file to server and gunzip

Linux server, how to do this? I'm stuck

         

badbadmonkey

9:47 am on Jun 11, 2008 (gmt 0)

10+ Year Member



I want to set-up a script to auto download the MaxMind database at
[maxmind.com...]
then unzip and replace the existing .dat file already on the server.

I got as far as

exec('gunzip -f GeoIP.dat.gz', $ret);

but that doesn't seem to work, don't do nothing at all.

I'm not used to working with file ops so I'm stuck. Did search but couldn't find anything helpful.

PHP 4.something on Linux and Apache.

Help?

badbadmonkey

10:22 am on Jun 11, 2008 (gmt 0)

10+ Year Member



Just to expand, even with -v (verbose) on gunzip I get a blank response. Nothing happens. The full code at the moment is like this:


exec('gunzip -f -v GeoIP.dat.gz', $ret);
foreach($ret as $line) {
echo $line;
echo "<br />";
}

Both the .gz and the old .dat files are in the same directory. It makes no difference if the old .dat is gone.

gunzip is there though and runs, if I run it with a bogus flag for instance, I get the help summary as output.

Little_G

10:43 am on Jun 11, 2008 (gmt 0)

10+ Year Member



Hi,

Take a look at PHP's compression wrappers: PHP: Compression Streams - Manual [php.net]

Andrew

badbadmonkey

11:44 am on Jun 11, 2008 (gmt 0)

10+ Year Member



I don't get it. I don't want to open the file, stream its contents, or do anything with it. All I want to do is:
- Copy .gz file from remote server
- Extract contents
- Replace existing .dat file
Finished.

Little_G

12:35 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



With PHP:
<?php
$data = file_get_contents('compress.zlib://http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz');
file_put_contents('GeoIP.dat', $data);
?>

badbadmonkey

1:12 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



Thanks but file_put_contents is PHP 5.

I also get an error from get_contents.

Why is it necessary to screw around with this streaming business? All I want to do is a simple file operation.

Little_G

1:52 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



Hi,

Ok, if downloading and decompressing the file is all you want to do then maybe we can skip PHP altogether.

This command will download the file with wget and decompress it with gzip and write it to a file:

wget -q http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz -O- ¦ gzip -d - > GeoIP.dat

change the ¦ to a pipe character. (the forum modifies this character)

You may not have wget available depending on what distribution of linux you are using, in that case you can use curl or lynx instead.

Andrew

badbadmonkey

4:05 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



Mmmn thanks. I will try that and set it as a cron job if it's the easiest way.

But one reason I wanted this set up in PHP is incase I want to add any logic to it later. I also don't have direct access to the console, so I wanted to get them to set up a cron and then I know I can edit the PHP any time I feel like it.

Can that command line above be exec'ed in PHP?

Little_G

4:17 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



Hi,

Yes you should be able to exec it.
You can always put the command in a shell script which can handle some added functionality, and you could call PHP from there if you decide you need it later.

Andrew

badbadmonkey

6:37 am on Jun 12, 2008 (gmt 0)

10+ Year Member



Well this is really bizarre.
It just won't work.
curl neither.
Same problem as trying to use exec on gunzip directly, just does nothing and returns no output.

eelixduppy

3:09 pm on Jun 12, 2008 (gmt 0)



Getting any errors?

badbadmonkey

4:13 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



No output at all. The return from exec() is blank.

Little_G

4:58 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Hi,

The command shouldn't return anything, check your document root for the file.

Andrew

badbadmonkey

5:11 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Sorry, no, nothing.
I echo'ed timestamps before and after the exec(), they were identical. So unless PHP sets up the exec() in its own process and proceeds to execute the script, it ain't doin' nuthin'...
Hence why I would expect an error.

Little_G

10:04 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Hi,

Try running something like:

<?php
header("Content-Type: text/plain");
$output; $returnVal;
exec('wget -V', $output, $returnVal);
var_dump($output, $returnVal);
unset($output,$returnVal);
exec('gzip -V', $output, $returnVal);
var_dump($output, $returnVal);
?>

and see what you get.

Andrew

badbadmonkey

2:17 am on Jun 25, 2008 (gmt 0)

10+ Year Member



One gets...

array(0) { 
}
int(126)
array(11) {
[0]=>
string(10) "gzip 1.3.5"
[1]=>
string(12) "(2002-09-30)"
[2]=>
string(39) "Copyright 2002 Free Software Foundation"
[3]=>
string(36) "Copyright 1992-1993 Jean-loup Gailly"
[4]=>
string(47) "This program comes with ABSOLUTELY NO WARRANTY."
[5]=>
string(43) "You may redistribute copies of this program"
[6]=>
string(50) "under the terms of the GNU General Public License."
[7]=>
string(69) "For more information about these matters, see the file named COPYING."
[8]=>
string(20) "Compilation options:"
[9]=>
string(83) "DIRENT UTIME STDC_HEADERS HAVE_UNISTD_H HAVE_MEMORY_H HAVE_STRING_H HAVE_LSTAT ASMV"
[10]=>
string(28) "Written by Jean-loup Gailly."
}
int(0)

badbadmonkey

2:20 am on Jun 25, 2008 (gmt 0)

10+ Year Member



Sorry for the delay by the way, I had other computer problems here recently...
Do appreciate the help. I'm just completely stumped, nothing I'm seeing makes any sense.

badbadmonkey

2:44 am on Jun 25, 2008 (gmt 0)

10+ Year Member



Hey hey!
I just tried compression wrappers under PHP 5 and it works fine.
So maybe I'll get them to switch the site over to 5, apparently they can, subject to testing the rest of it for upgrade compatibility.

Would still be curious to know why the exec() isn't working?

Little_G

8:11 am on Jun 25, 2008 (gmt 0)

10+ Year Member



Hi,

The result you get implies that you don't have wget (in case you didn't already know that). You can alter the code to test for curl.

Andrew

badbadmonkey

8:42 am on Jun 25, 2008 (gmt 0)

10+ Year Member



Yeah I did it made no difference, although I spent less time on that. I might go back and give it another look.