Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Streaming file through php server from amazon s3

I would like to stream a file from S3 to the client browser using PHP. I could manage to do it but I have a problem with large files. First, it takes all the memory available for the session and end up with a "Memory exhausted" error. Second, when reaching "fread(...)" for the first time, it hangs almost 1 min for a 50Mo file before to start sending to the browser. How can I handle these 2 problems? Here is my code.

$client->register_stream_wrapper();

    if($stream = fopen('s3://' . S3_BUCKET . '/' . $destination_s3, 'r'))
    {
        //Send file to browser.
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Cache-control: no-store");
        header("Pragma: no-cache");
        header("Content-Disposition: attachment; filename=$file_name");
        header("Content-Type: " . $file_type);
        while (!feof($stream)) {
            echo fread($stream, 1024);
            flush();
        }
        fclose($stream);
    }
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!