Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:49:39+00:00 2026-06-16T07:49:39+00:00

How do I stop a remote file from downloading a file if it exceeds

  • 0

How do I stop a remote file from downloading a file if it exceeds 5mb? If I stop it while transferring, will the file be held somewhere in some other temp dir or memory? How would I know? Here is my current code:

$url = 'http://www.spacetelescope.org/static/archives/images/large/heic0601a.jpg';
$file = '../temp/test.jpg';
file_put_contents($file, file_get_contents($url));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:49:40+00:00Added an answer on June 16, 2026 at 7:49 am

    There are a number of ways in which you could do this, but because you are currently using file_get_contents(), here is what I would do:

    • Open the remote file using fopen()
    • Read the file and save it in increments using fread() – this allows you to track the current size and throw and error when you pass the threshold.

    Something like this:

    $url = 'http://www.spacetelescope.org/static/archives/images/large/heic0601a.jpg';
    $file = '../temp/test.jpg';
    $limit = 5 * 1024 * 1024; // 5MB
    
    if (!$rfp = fopen($url, 'r')) {
      // error, could not open remote file
    }
    if (!$lfp = fopen($file, 'w')) {
      // error, could not open local file
    }
    
    // Check the content-length for exceeding the limit
    foreach ($http_response_header as $header) {
      if (preg_match('/^\s*content-length\s*:\s*(\d+)\s*$/', $header, $matches)) {
        if ($matches[1] > $limit) {
          // error, file too large
        }
      }
    }
    
    $downloaded = 0;
    
    while (!feof($rfp) && $downloaded < $limit) {
      $chunk = fread($rfp, 8192);
      fwrite($lfp, $chunk);
      $downloaded += strlen($chunk);
    }
    
    if ($downloaded > $limit) {
      // error, file too large
      unlink($file); // delete local data
    } else {
      // success
    }
    

    NB: it is tempting to inspect the Content-Length: header before you receive any of the file to check whether the file is too large – you can still do this if you like, but do not trust the value! The header is essentially an arbitrary value, and while it would be a protocol violation to use a content length that does not match the size of the file, it could be used to fool the system into downloading a file that breaks the rules. You’d need count the bytes even if you do check this value.

    You could also do this using curl via the data callback, but I would regard this as a less satisfactory solution, mostly because of fact that curl requires a string function name and not the standard generic callable type, which means you would either need to use a global variable or a static variable in order to keep track of the downloaded content length, neither of which are acceptable (IMHO) for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Will a thread stop if I send it SIGTSTP signal? Or in other words
I'm downloading a perl CGI file from a Linux web server. It has Unix
I created a basic installer. It takes a file from c:\temp.log and copy it
I want to stop the end users from being able to resize a GUI
How do I stop my application from going back when the user clicks on
how to I stop a Video from playing and go back to start? videoPlayer.currentPlaybackTime
For my project I have to communicate from my (php) website to remote devices.
I currently have a PHP file which will search my MySQL database and see
I want to remove stop words in java. So, I read stop words from
I need to send a file from one PHP page (on which client uploads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.