Jump to content

Recommended Posts

I'm using the following code for users to download files from my website. It works fine for files under 50MB, but fails immediately for larger files. Can someone explain why this is happening and how to fix it? Some of the downloadable files are over 800MB, so I need this working. 

NOTE: No error messages are thrown on large files.

<?php
 error_reporting(E_ALL);
 ini_set('display_errors', '1');

$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';

$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{	echo "unable to connect";
   exit;
}

function mydloader($l_filename=NULL){    
    if( isset( $l_filename ) ) {
        $filename = preg_replace("/\s+/u", " ", $l_filename);
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if  ($ext == '.deb')
            header('Content-Type: octet-stream');
        elseif ($ext == '.iso')
            header('Content-Type: application/x-cd-image');
        elseif ($ext =='.gz')
            header('Content-Type: application/zip');
        else
            header('Content-Type: octet-stream');

            header('Content-Description: File Transfer');
            header("Content-Disposition: attachment; filename={$filename}");
            header('Pragma: public');
            header('Expires: 0');    
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filename));   
            readfile($filename);
    
 
        // Get lookup id
        $test = $pdo->query("SELECT lookup.id FROM lookup WHERE inet_aton('$ip') >= lookup.ipstart AND inet_aton('$ip') <= lookup.ipend");
        $ref = $test->fetchColumn();
        $ref = intval($ref);

        // Insert record in download table
        $stmt = $pdo->prepare("INSERT INTO download (`address`, `filename`, `ip_address`, `lookup_id`) VALUES (?, ?, inet_aton('$ip'),?)");
        $stmt->execute([$ip, $ext, $ref]) ; 
      }
        
    else {
        echo "isset failed";
        }  
}      
mydloader($_GET["f"]);
    

Thanks in advance
 

Link to comment
https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/
Share on other sites

Try changing this line:

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

AND - I don't believe that you are not getting any error message.  You have not defined the pdo connection inside that misplaced function that is trying to do a query and update.

Edited by ginerjm

Nor is $ip defined inside the function.  That is 2 errors you have besides the line I addressed.

This is what I get when I run an edited version of your script:


Notice: Undefined variable: ip in /home/albany/public_html/homejg/test.php on line 58

Notice: Undefined variable: ip in /home/albany/public_html/homejg/test.php on line 59

Notice: Undefined variable: pdo in /home/albany/public_html/homejg/test.php on line 60

Fatal error: Uncaught Error: Call to a member function query() on null in /home/albany/public_html/homejg/test.php:60 Stack trace: #0 /home/albany/public_html/homejg/test.php(30): mydloader('file.deb') #1 {main} thrown in /home/albany/public_html/homejg/test.php on line 60

 

Edited by ginerjm

the errors are being appended to the end of the downloaded file.

in fact, there may be a php error appended to the end of a large file that would explain why it isn't working.

for a file download application, you would want to log all php errors.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.