www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP Download function

  1. #1
    Join Date
    Dec 2013
    Posts
    3

    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 MB). 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);
    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!!

  2. #2
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    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...s-through-php/

  3. #3
    Join Date
    Dec 2013
    Posts
    3
    Quote Originally Posted by mistin.ca View Post
    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...s-through-php/
    Thank you, Mistin

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

    I'll get cracking.

  4. #4
    Join Date
    Mar 2011
    Posts
    1,137
    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.
    Rick Trethewey
    Rainbo Design

  5. #5
    Join Date
    Dec 2013
    Posts
    3
    Quote Originally Posted by rtrethewey View Post
    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.
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles