File download causes site to not respond

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
welly
Forum Newbie
Posts: 5
Joined: Mon Jan 21, 2008 3:34 pm

File download causes site to not respond

Post 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;
 
  } 
 
?>
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: File download causes site to not respond

Post by VladSun »

Shared hosting? If yes, then maybe mod_bandwidth is used.
There are 10 types of people in this world, those who understand binary and those who don't
welly
Forum Newbie
Posts: 5
Joined: Mon Jan 21, 2008 3:34 pm

Re: File download causes site to not respond

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: File download causes site to not respond

Post by Mordred »

Maybe there is a $_SESSION['downloading'] condition somewhere in the other code?

Also, you have SQL injection on $_GET['id']
welly
Forum Newbie
Posts: 5
Joined: Mon Jan 21, 2008 3:34 pm

Re: File download causes site to not respond

Post 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!
Post Reply