views:

20

answers:

2

I'm finding it difficult to phrase this question correctly, let me try to explain our problem...

We have an intranet running on Ubunutu box with Apache2/PHP 5.2.4. We have a bit of PHP code that reads a file from a directory that is not publically accessible and output it to the screen (code below):

$file_path = '/home/path/to/filename.gif';

if(file_exists($file_path)){
    $output = FALSE;

    //File Information
    $path_parts = pathinfo($file_path);
    $file_size = filesize($file_path);
    $file_ext = (isset($path_parts['extension'])) ? strtolower($path_parts['extension']) : null;    
    $file_name = $path_parts['basename'];        

    //Sets up the headers    
    if($file_size > 0){
        header('Content-Length: ' .$file_size);
    }                    
    header('Content-Disposition: attachment; filename="'.$file_name.'"');
    header('Content-Type: application/octet-stream');


    //Reads the File
    if($file_size > 0){
        $handle = fopen($file_path, "r");
        $output = fread($handle, $file_size);
        fclose($handle);
    }

    //Outputs the File
    echo $output;
}

Inside our network when, browsing to the page that uses this code, the file is downloaded perfectly and quickly... However, when accessing this page via our Cisco ASA/Proxy/VPN (not sure what to call it) this code locks up the browser, but does eventually download the file...

After a bit of experimenting, after taking out the headers and just echoing the contents of the file to the browser, it prints no problem. However as soon as I add the lines with the headers back into the code it causes the hanging again, but only when accessed via this box..

Anybody come across this problem before or have any idea what we can try to move forward?

Thanks for any advice...

A: 

Have you tried eliminating the content-size header entirely? The proxy may be taking that as a firm promise and if the data you're sending ends up being a different size, the proxy may wait for those last few "missing" bytes to show up.

Just as an aside, you should use [readfile()][1] instead of the fopen()/fread()/echo construct you have now.

As it stands now, you're slurping the contents of the entire file into memory and then echoing out. For large files and multiple requests, you'll kill the server with memory starvation. readfile will automatically stream the file in smaller chunks so that memory usage is minimal.

Marc B
Thanks for the readfile() tip... I have tried eliminating the contents size header entirely... no avail :-(
Jenski
A: 

Your proxy obviously have problems with the Content-Type: application/octet-stream. Try setting it to the real MIME-type of each file. You can use the Fileinfo module to find out which MIME-type a certain file is, like this:

//You may need to specify the location of your system's magic file
//See http://php.net/finfo_open for more info
$finfo = new finfo(FILEINFO_MIME);
$mimetype = $finfo->file($file_path);
Emil Vikström