Managing PHP's readfile() function for efficiently handling large file downloads

As I set up an online file management system, I encountered a roadblock.

My attempt to send the file to the client using this customized readfile function seems to be causing issues:

function readfile_chunked($filename,$retbytes=true) { 
   $chunksize = 1*(1024*1024); // chunk size in bytes 
   $buffer = ''; 
   $cnt =0; 
   $handle = fopen($filename, 'rb'); 
   if ($handle === false) { 
       return false; 
   } 
   while (!feof($handle)) { 
       $buffer = fread($handle, $chunksize); 
       echo $buffer; 
       ob_flush(); 
       flush(); 
       if ($retbytes) { 
           $cnt += strlen($buffer); 
       } 
   } 
       $status = fclose($handle); 
   if ($retbytes && $status) { 
       return $cnt;
   } 
   return $status; 
}

Unfortunately, when attempting to download a 13 MB file, it stops at 4 MB. Any ideas on what could be causing this issue? Speed and time limits are not problem as I'm working on a local network with sufficient resources.

I have confirmed that PHP's memory limit is set to 300 MB.

Appreciate any assistance you can provide.

Answer №1

It seems probable that you are encountering the response buffer limit enforced by your server. Commonly, IIS and FastCGI have a default buffer size of 4mb. To resolve this issue, I recommend examining the configuration between the webserver and PHP SAPI.

Answer №2

Avoid flushing your output individually and instead buffer it to send to the client all at once.

ob_clean(); // clear any previous buffers
ob_start();

// perform read/echo operations

ob_end_flush();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to incorporate JavaScript into all pages of a website?

Interested in incorporating "Google Analytics" on your website? I just finished configuring my Google Analytics, but the final step is to insert the tracking code into every page I want to track. The code provided is: <script> (function(i,s,o,g,r ...

Utilize a stored string as the destination for the content of an object

We are currently working on processing a large amount of json data and trying to specify which parts of it to use using string variables. My goal is to convert a string into an object path to access the content of an item. The following code works correc ...

How can I retrieve the href attribute from the webpage?

I am in need of extracting the href attribute from the following HTML snippet: <tr> <td><h2 class="officer"><a href="/finance/stocks/officerProfile?symbol=ABB.N&officerId=232795" class="link">Roger&nbsp;Agnelli</a> ...

Transmit ajaxform information to several email addresses

I am looking for a way to send the data filled out by the visitor to both myself and the visitor. Can you please provide some guidance on how to do this? My email address is [email protected] and the visitor's email is $email The code snippet i ...

Move and place, editing tools

Is it possible to create a drag-and-drop editor similar to the one found in the form editor on wufoo.com? ...

Unable to implement a delegate method effectively

I am currently facing an issue with using Ajax in a bootstrap PHP page. The problem is that I cannot use functions inside the onsubmit event because the button does not exist beforehand. Therefore, I have to resort to using delegate methods instead. Howeve ...

Creating a single object from an array using Zend_Soap_Client

I'm currently facing an issue with Zend_Soap_Client while working on my project: <parent> <child><name>abc</name></child> <child><name>def</name></child> </parent> When there are mu ...

Troubleshooting Laravel 5 Routing Problem with CSS Directory

So, I have this route that loads a page, but for some reason, the css on the page doesn't load properly. Route::get('admins', function () { return view('admins/index'); }); Interestingly, when I use a different route with ...

Using PHP script to retrieve MySQL table values and dynamically update a live graph in a Javascript function

I've been researching similar issues for quite some time now, but to no avail. Currently, I'm utilizing Highcharts to update a graph every 3 seconds with the latest entry from a specific MySQL table. I am referring to the example Javascript code ...

Generating PHP objects at runtime using a string-based approach

Is there a way to create an object in PHP based on a type specified by a string stored in a MySQL database? The table contains columns and sample data like: id | type | propertyVal ----+------+------------- 1 | foo | lorum 2 | bar | ipsum ...and I ...

I'm struggling to formulate the correct query using mysql and php

I currently have four tables stored in my database: programme https://i.stack.imgur.com/IUk8z.jpg course https://i.stack.imgur.com/N2NPi.jpg users https://i.stack.imgur.com/tATDg.jpg payment https://i.stack.imgur.com/Wnl48.jpg I am looking to retr ...

AJAX (Vanilla JavaScript): Sending Image through HTTP Request

I am a beginner with ajax and prefer not to use frameworks. I attempted to use PHP to determine if a file is set and display either true or false, but it didn't work as expected. I'm unsure of where I went wrong. Below is my HTML and JS code: & ...

Loading SVG images in advance

I am in possession of around one hundred simple SVG images, distributed among five different image folders. These images are currently retrieved on demand when they need to be displayed, which generally works well but occasionally results in flickering tha ...

Access values from the view in Blade without passing them as parameters

Is there a way to retrieve a variable named "foo" from a Blade template and pass it to a static function defined within the same template? For instance, let's say we have: <div class="collumns large-8"> {{ Helpers::setLetters('variable_n ...

Regular expressions are compatible with JavaScript, but unfortunately, they are not supported

After successfully implementing this regex in my JavaScript for webpage validation, I attempted to incorporate it into my PHP script as a backup. However, every string I passed through the regex failed, even correct names. In an effort to resolve the issu ...

Nginx's sluggish SSL downloads at a snail's pace

I have configured this virtual host: server { server_name admin.ex.com ; listen 80 ; listen [::]:80 ; ##SSL #listen 443 ssl ; listen *:443 ssl http2 ; listen [::]:443 ssl http2 ; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #s ...

The function password_verify() consistently yields a negative result

As a beginner, I recently attempted to develop a login system using PHP and MySQL. I successfully created a registration form and added a few users, but encountered issues when trying to create the login form. Every time I tried to log in, it displayed an ...

Using jQuery AJAX to Transfer Data to a PHP Script

There is a div element with a data-id attribute. The goal is to use jQuery to detect when this div is clicked and then send or store the corresponding data-id value in a PHP file. However, the code provided raises an error when trying to obtain the data-id ...

Utilizing long polling technique with jQuery/AJAX on the server end

Currently, I am facing an issue with long polling on a single page that contains multiple pages. The problem arises when a new request is made while a previous request is still processing. Even though I attempt to abort the previous request, it completes b ...

`The Conundrum of Basic HTML Parsing`

Hello, I am currently working on extracting professor names and comments from the ratemyprofessor website by converting each div into plaintext. The following is the structure of the div classes that I am dealing with: <div id="ratingTable"> <div ...