Page 1 of 1

File download causes site to not respond

Posted: Tue Jan 22, 2008 5:23 pm
by welly
Hi guys,

I've been working with some code I found on php.net, for downloading files, which is working mostly correctly. However, when the file download starts, the user is unable to browse any more of the site. I did test this with two separate clients - I am connected to the server with my laptop and another user is connect to the site with another machine. He is able to browse the site as normal.

The code I'm using is below. If anyone has any suggestions what might be causing this problem, that would be greatly appreciated!

Cheers,

Welly

Code: Select all

 
<?php
 
    require_once('./library/defaults.inc.php');
    require_once('./library/acl.inc.php');
    
    // The sendFile function streams the file and checks if the 
    // connection was aborted. 
    function sendFile($path, $contentType = 'application/octet-stream') { 
 
      ignore_user_abort(true); 
      header('Content-Transfer-Encoding: binary'); 
      header('Content-Disposition: attachment; filename="' . basename($path) . "\";"); 
      header("Content-Type: $contentType"); 
        header("Content-Length: ".(string)(filesize($path)));
 
      $res = array( 
        'status' => false, 
        'errors' => array(), 
        'readfileStatus' => null, 
        'aborted' => false 
      ); 
 
      $res['readfileStatus'] = readfile($path); 
 
      if ($res['readfileStatus'] === false) { 
        $res['errors'][] = 'readfile failed.'; 
        $res['status'] = false; 
      } 
 
      if (connection_aborted()) { 
        $res['errors'][] = 'Connection aborted.'; 
        $res['aborted'] = true; 
        $res['status'] = false; 
      }
 
      return $res; 
    }
 
    $id = $_GET['id'];
 
    $result = $db->Execute("SELECT vt.* FROM queue q INNER JOIN video_types vt ON q.video_type_id = vt.id WHERE q.id = $id");   
    $file_name = $result->fields['file_name'];
 
  $res = sendFile($file_store.$file_name, 'application/octet-stream'); 
 
  if ($res['status']) { 
 
    // Download succeeded
        $_SESSION['downloading'] = false;
    
  } else { 
 
        // Download failed
        $_SESSION['downloading'] = false;
 
  } 
 
?>
 

Re: File download causes site to not respond

Posted: Tue Jan 22, 2008 6:15 pm
by VladSun
Shared hosting? If yes, then maybe mod_bandwidth is used.

Re: File download causes site to not respond

Posted: Tue Jan 22, 2008 10:58 pm
by welly
No, this is on a local development machine - while it's only a mini mac (with OS X Leopard Server), it's only me and very occasionally one other person, accessing the machine.

Re: File download causes site to not respond

Posted: Wed Jan 23, 2008 12:40 am
by Mordred
Maybe there is a $_SESSION['downloading'] condition somewhere in the other code?

Also, you have SQL injection on $_GET['id']

Re: File download causes site to not respond

Posted: Wed Jan 23, 2008 1:24 pm
by welly
Ok, can't explain this one but it seems to be working now! It could well be that my web client was doing something else at the time and the default 2 connections were taken up.

thanks for the sql injection tip! I was going to go over the security side of this site near then end and make any fixes such as this then. just working on functionality at the moment!