j8d
j8d

Reputation: 510

Downloading a 2MB File in PHP

I'm writing a function that updates a database file every 30 days. It works great for small files, but for files over 200K or so, it just downloads a partial file. How can I make this work with files up to 2MB?

function lsmi_geoip_update() {
    $dir = dirname( __FILE__ );
    $localfilev4 = $dir . '/data/GeoIPv4.dat';
    $localfilev6 = $dir . '/data/GeoIPv6.dat';
    if ( file_exists( $localfilev4 ) ) {
        rename($dir . '/data/GeoIPv4.dat', $dir . '/data/OLD_GeoIPv4.dat');
        $newfilev4 = file_get_contents('http://gdriv.es/geoipupdate/GeoIPv4.dat');
        file_put_contents($dir . '/data/GeoIPv4.dat', $newfilev4);
        // unlink($dir . '/data/OLD_GeoIPv4.dat');
    }
    if ( file_exists( $localfilev6 ) ) {
        rename($dir . '/data/GeoIPv6.dat', $dir . '/data/OLD_GeoIPv6.dat');
        $newfilev6 = file_get_contents('http://gdriv.es/geoipupdate/GeoIPv6.dat');
        file_put_contents($dir . '/data/GeoIPv6.dat', $newfilev6);
        // unlink($dir . '/data/OLD_GeoIPv6.dat');
    }
}

Here's the output:

enter image description here

Upvotes: 0

Views: 260

Answers (3)

j8d
j8d

Reputation: 510

Hosting the files on a different server fixed it.

Upvotes: 1

Collapsed PLUG
Collapsed PLUG

Reputation: 304

Edit php.ini to allow bigger upload first. Add (or find & edit) upload_max_filesize = 2M to your php.ini.

If that doesn't work, consider file_get_contents (= downloading) failing in the first place. Your connection could be timing out before it can fetch the whole data. Try this. (Sorry I can't comment everywhere yet that I had to answer on guessing)

$ctx = stream_context_create(array( 
    'http' => array( 
        'timeout' => 120 
        ) 
    ) 
); 
file_get_contents("http://gdriv.es/geoipupdate/GeoIPv4.dat", 0, $ctx); 

Upvotes: 1

bhrached
bhrached

Reputation: 137

you must change the setting in the php.ini upload_max_filesize = 2M and max_execution_time = X according to your need.

Upvotes: 1

Related Questions