/    Sign up×
Community /Pin to ProfileBookmark

PHP Download function

I have a form with three fields; Name, Organisation and Email. And a submit button.

When you hit submit, it writes the three things to a database and invokes the download of a file.

This all works as expected, however after a seemingly random length of time (between 3 and 10 minutes) the file stops downloading. No error or timeout messages, just stops as if it has successfully downloaded.

The file is a large exe (360 M?. I presume it is something to do with the PHP.

The code is:

[CODE]$filename = “myfile.exe”;

header(“Content-Length: ” . filesize($filename));
header(‘Content-Type: application/x-msdownload’);
header(‘Content-Disposition: attachment; filename=myfile.exe’);

readfile($filename);[/CODE]

My questions are;

  • 1.

    If I right click on a standard link and ‘save file as’ it downloads just fine. If the PHP script has done it’s job and now the file is downloading, what would the previous PHP have to do with it?

  • 2.

    What would be the correct code? I have read the manual but it serves only to confuse me mainly because of my limited PHP knowledge I guess.

  • Thank you so much for any replies!!

    to post a comment
    PHP

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @mistin_caDec 13.2013 — readfile() will exhaust memory, you need to send the data to browser in chunks (keeping memory limit down) then it only depends on timelimit.

    Look into PEAR HTTP_DOWNLOAD or search for a "readfile chunked" function that includes flushing.

    Quick google search for chunked dl returns this which looks like it will suit your needs: http://teddy.fr/2007/11/28/how-serve-big-files-through-php/
    Copy linkTweet thisAlerts:
    @kaywertauthorDec 13.2013 — readfile() will exhaust memory, you need to send the data to browser in chunks (keeping memory limit down) then it only depends on timelimit.

    Look into PEAR HTTP_DOWNLOAD or search for a "readfile chunked" function that includes flushing.

    Quick google search for chunked dl returns this which looks like it will suit your needs: http://teddy.fr/2007/11/28/how-serve-big-files-through-php/[/QUOTE]


    Thank you, Mistin

    That's a great help and a good place to start.

    I'll get cracking.
    Copy linkTweet thisAlerts:
    @rtretheweyDec 13.2013 — Serving the file in chunks/blocks may not be the solution. There is a maximum execution time limit for PHP scripts, and your server may not allow a script to run long enough to send a 360MB file, no matter how your script tries to send it.

    For files that large, you should probably look into setting up a means of allowing authorized users to directly download the file though a conventional HTML/HTTP link. Try looking for a file management script in one of the script directory sites.
    Copy linkTweet thisAlerts:
    @kaywertauthorDec 13.2013 — Serving the file in chunks/blocks may not be the solution. There is a maximum execution time limit for PHP scripts, and your server may not allow a script to run long enough to send a 360MB file, no matter how your script tries to send it.

    For files that large, you should probably look into setting up a means of allowing authorized users to directly download the file though a conventional HTML/HTTP link. Try looking for a file management script in one of the script directory sites.[/QUOTE]


    Hi, rtrethewey

    I have decided to do just what you suggested. I'm still unsure, but after a long afternoon of research, all signs point to a server / script time issue. I read somewhere that most shared hosting will not allow a script to execute for such a length of time.

    I have setup the form to do its thing and then load another page with a standard html link. It works just fine, but just not as sleek as I would of liked it!

    Thanks for all your pointers though, guys.

    K
    ×

    Success!

    Help @kaywert spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...