foxclone Posted March 14, 2022 Share Posted March 14, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/ Share on other sites More sharing options...
ginerjm Posted March 14, 2022 Share Posted March 14, 2022 What in god's name does this code have to do with doing a file download????? Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594386 Share on other sites More sharing options...
foxclone Posted March 14, 2022 Author Share Posted March 14, 2022 The section with the headers ending with readfile($filename) is the download code. Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594387 Share on other sites More sharing options...
ginerjm Posted March 14, 2022 Share Posted March 14, 2022 (edited) 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 March 14, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594388 Share on other sites More sharing options...
ginerjm Posted March 14, 2022 Share Posted March 14, 2022 (edited) 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 March 14, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594389 Share on other sites More sharing options...
foxclone Posted March 14, 2022 Author Share Posted March 14, 2022 Amazingly, it threw no errors. It also wasn't updating the db. It's been corrected. Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594390 Share on other sites More sharing options...
ginerjm Posted March 14, 2022 Share Posted March 14, 2022 The code you posted above has those errors in it. It Has to throw errors. I suggest that the code you posted it NOT the code you are running. It happens. Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594391 Share on other sites More sharing options...
mac_gyver Posted March 14, 2022 Share Posted March 14, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594392 Share on other sites More sharing options...
ginerjm Posted March 14, 2022 Share Posted March 14, 2022 And I commented out the readfile line which is why they DID show up on my screen... Quote Link to comment https://forums.phpfreaks.com/topic/314598-problem-downloading-files-over-50mb/#findComment-1594393 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.