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 7546507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:13:00+00:00 2026-05-30T09:13:00+00:00

Small script, being used to send one of two .pdf files to the user

  • 0

Small script, being used to send one of two .pdf files to the user based on the get string:

if ($fileToSend == "bigone") { 
   $filename = "largefile.pdf";
   $header = "application/pdf";
} else if ($fileToSend == "smallone") { 
   $filename = "smallfile.pdf";
   $header = "application/pdf";
}
if (file_exists('d/' . $filename)){
    header('Content-type: ' . $header);
    header('Content-disposition: attachment; filename=' . $filename);   
    readfile('d/' . $filename);
} else { 
    echo('No good');
}

Both files d/smallfile.pdf and d/largefile.pdf exist. smallfile.pdf is about 5megs. largefile.pdf is about 25megs.

sendFile.php?fileToSend=smallone works.

sendFile.php?fileToSend=bigone doesn’t work; in FF, it says

File not found
Firefox can’t find the file at [url]/sendFile.php?fileToSend=bigone

There are no filename errors.

I’ve tried

  • Adding flush(); prior to the readfile; this causes the download file dialogue box to appear, but then serves a 0 byte file
  • Adding flush(); prior to the readfile, and adding header(‘Content-Length: ‘ . filesize(‘d/’ . $filename)); – this works successfully, but takes close to 2 minutes to process (ie, once I hit the URL, nothing happens for close to 2 minutes, and then the dialogue box appears with the actual file to download in it correctly).

Any help would be much appreciated.
Jon

  • 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-05-30T09:13:01+00:00Added an answer on May 30, 2026 at 9:13 am

    Yes your problem is most likely php timing out while feeding you the file, a remedy for this is to feed you chunks so as not to chew memory and set_time_limit(0); so it wont timeout after 60s:

     <?php 
     if(isset($_GET['fileToSend'])){
    
        switch($_GET['fileToSend']){
            case "bigone":
                if(downloadFile("./d/largefile.pdf")===false){
                    //failed, do somthing
                }
                break;
    
            case "smallone":
                if(downloadFile("./d/smallfile.pdf")===false){
                    //failed, do something
                }
                break;
    
            default:
                echo 'No file selected';
                break;
    
        }
    
     }
    
     function downloadFile($file){
        if (file_exists($file)) {
            if(is_dir($file)){return false;}
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file)).'"';
            header('Content-Transfer-Encoding: binary');
            header('Connection: Keep-Alive');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
    
            ob_clean();
            $handle = fopen($file, "rb");
            $chunksize=(filesize($file)/1024);
    
            set_time_limit(0);
            while (!feof($handle)) {
                echo fgets($handle, $chunksize);
                flush();
            }
            fclose($handle);
            die;
        }else{
            return false;
        }
     }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting to work on a small script that takes a string, counts the
Im making a small python script to upload files on the net. The script
We have a script used to edit certain files inline. Basically, each file is
I have written small bash script to try to get the last x characters
Yesterday I made a small script with some help to read .csv files. I
I'm trying to write a small Python script that will get query results from
I have a small script and want to detect the script version from admin
I have a small script which im using to test PHP mail(), as below:
I have a small script that I am try to port to work for
I'm trying to write a small script to change the current directory to my

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.