Help! Streaming files via PHP get truncated...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Good Man

    #1

    Help! Streaming files via PHP get truncated...

    Hi there folks

    I've got an interesting little problem going on. On one of my projects,
    I have users log in to retrieve files. The files themselves are stored
    outside of the www directory on the server, so the only way they can be
    downloaded is for PHP to stream them to the user.

    My problem is that my downloads are always "completing " too soon, and
    always around 1.6 -> 1.9 MB. So, when I try to download a 50MB file,
    I'm told that the download is complete wayyy to early, and of course, the
    file is nothing but garbage.

    I have another script on the same server that will zip together a bunch
    of files on the fly and stream it to the browser using a class that i did
    not write personally. If I tell my system I want to download that 50MB
    file as a .zip file, it works without a problem. If I tell the system I
    want to download the 50MB file normally (its a PDF) using my
    'stream.php' file, it truncates. ACK!

    some variables that I got from phpInfo(); which may/may not have anything
    to do with this problem (maybe this will help someone/me?):

    output_bufferin g 4096 4096
    max_execution_t ime 210 210
    max_input_time 120 120

    Anyways, here's the essence of the script I'm using - it's worked on
    other servers (where i've been able to download large files
    successfully).. .. can anyone offer any suggestions as to why the
    downloads are truncating? or any other methods of doing what I want to
    do?

    thanks. try to ignore the word-wrap! oh... PHP Version 5.0.4
    (www.entropy.ch Release 1) on a Mac PPC, running apache.

    *******

    <?php
    session_cache_l imiter("must-revalidate");
    session_start() ;

    /* downloading a file */

    @$vKey = addslashes($_RE QUEST['vKey']);

    if ($vKey=="") { //no ID?
    popup("We are having trouble locating this specific file.",$vBackPa ge);
    exit;
    }

    databaseconnect (); //custom function connects to the database

    //get the file
    $row = singlequery("SE LECT f.FilePath,f.Fi leName FROM Files AS f INNER
    JOIN Folders ON f.FolderID = Folders.FolderI D INNER JOIN Projects ON
    Folders.Project ID = Projects.Projec tID WHERE f.FileKey='$vKe y'");
    }

    $vFileName = $row["FileName"];
    $vFilePath = $row["FilePath"];

    if($vFileName== "") {
    popup("The file does not exist, or you do not have access to it.",
    $vBackPage);
    }

    //now we stream the file, prompting a download
    header("Cache-control: private");

    // We'll be outputting a file
    header('Content-Type: application/octet-stream');

    // It will be called whatever the file name is called, and given the
    // attachment Disposition to force the download

    header('Content-Disposition: attachment; filename="'.$vF ileName.'"');

    // The source... see ya
    readfile($vFile Path);

    ?>


    many thanks,
    GM


  • Good Man

    #2
    Re: Help! Streaming files via PHP get truncated...

    Good Man <heyho@letsgo.c om> wrote in
    news:Xns9757D68 E58DABsonicyout h@216.196.97.13 1:

    [color=blue]
    > My problem is that my downloads are always "completing " too soon, and
    > always around 1.6 -> 1.9 MB.[/color]

    hey, guess who JUST read the readfile(); page in the manual? ;)

    Down in the comments section are the following posts... I've posted them
    here in case anyone is trying to solve this problem in the future and
    stumbled across this thread (hello future! do you have flying cars?)


    COMMENTS:
    ( http://ca.php.net/manual/en/function.readfile.php )

    flobee at gmail dot com
    06-May-2005 02:17
    regarding php5:
    i found out that there is already a disscussion @php-dev about readfile
    () and fpassthru() where only exactly 2 MB will be delivered. so you may
    use this on php5 to get lager files

    .... which eventually morphed via 'chrisputnam at gmai<snip>' into the
    following function, which i have used successfully. thanks chris!


    function readfile_chunke d($filename,$re tbytes=true) {
    $chunksize = 1*(1024*1024); // how many bytes per chunk
    $buffer = '';
    $cnt =0;
    // $handle = fopen($filename , 'rb');
    $handle = fopen($filename , 'rb');
    if ($handle === false) {
    return false;
    }
    while (!feof($handle) ) {
    $buffer = fread($handle, $chunksize);
    echo $buffer;
    ob_flush();
    flush();
    if ($retbytes) {
    $cnt += strlen($buffer) ;
    }
    }
    $status = fclose($handle) ;
    if ($retbytes && $status) {
    return $cnt; // return num. bytes delivered like readfile() does.
    }
    return $status;

    }

    Comment

    • d

      #3
      Re: Help! Streaming files via PHP get truncated...

      "Good Man" <heyho@letsgo.c om> wrote in message
      news:Xns9757DBE B57C92sonicyout h@216.196.97.13 1...[color=blue]
      > Good Man <heyho@letsgo.c om> wrote in
      > news:Xns9757D68 E58DABsonicyout h@216.196.97.13 1:
      >
      >[color=green]
      >> My problem is that my downloads are always "completing " too soon, and
      >> always around 1.6 -> 1.9 MB.[/color]
      >
      > hey, guess who JUST read the readfile(); page in the manual? ;)
      >
      > Down in the comments section are the following posts... I've posted them
      > here in case anyone is trying to solve this problem in the future and
      > stumbled across this thread (hello future! do you have flying cars?)[/color]

      Nicely done ;)
      [color=blue]
      >
      > COMMENTS:
      > ( http://ca.php.net/manual/en/function.readfile.php )
      >
      > flobee at gmail dot com
      > 06-May-2005 02:17
      > regarding php5:
      > i found out that there is already a disscussion @php-dev about readfile
      > () and fpassthru() where only exactly 2 MB will be delivered. so you may
      > use this on php5 to get lager files
      >
      > ... which eventually morphed via 'chrisputnam at gmai<snip>' into the
      > following function, which i have used successfully. thanks chris!
      >
      >
      > function readfile_chunke d($filename,$re tbytes=true) {
      > $chunksize = 1*(1024*1024); // how many bytes per chunk
      > $buffer = '';
      > $cnt =0;
      > // $handle = fopen($filename , 'rb');
      > $handle = fopen($filename , 'rb');
      > if ($handle === false) {
      > return false;
      > }
      > while (!feof($handle) ) {
      > $buffer = fread($handle, $chunksize);
      > echo $buffer;
      > ob_flush();
      > flush();
      > if ($retbytes) {
      > $cnt += strlen($buffer) ;
      > }
      > }
      > $status = fclose($handle) ;
      > if ($retbytes && $status) {
      > return $cnt; // return num. bytes delivered like readfile() does.
      > }
      > return $status;
      >
      > }
      >[/color]


      Comment

      Working...