Binary file transfer fails when > 2mb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #1

    Binary file transfer fails when > 2mb

    I want to download a WordPerfect document .wpd via my php app. I use the following code

    Code:
    	function downLoadFile ($fullPath)
    	{
    		/*
    			Usage:
    				<a href="download.php?download_file=some_file.pdf">Download here</a>
    		*/
    
    		if ($fd = fopen ($fullPath, "rb")) {
    		    $fsize = filesize($fullPath);
    		    $path_parts = pathinfo($fullPath);
    
    		    /*get file type from extention*/
    		    $ext = strtolower($path_parts["extension"]);
    
    			/* add here more headers for diff. extensions */
    		    switch ($ext) 
    			{
    		        case "pdf":
    			        header("Content-type: application/pdf"); 
    					/* use 'attachment' to force a download */
    					header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); 
    					break;
    
    		        default;
    			        header("Content-type: application/octet-stream");
    			        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    			        break;
    
    		    }
    		    header("Content-length: $fsize");
    		    header("Cache-control: private"); //use this to open files directly
    		    while(!feof($fd)) {
    		        $buffer = fread($fd, 2048);
    		        echo $buffer;
    		    }
    		}
    		fclose ($fd);
    		exit;
    	}
    We have several locations (buildings). When downloading a document in the building with the server, I have no problem, however if one of our remote locations attempts to download a document > 2mb it times out at 1.4 to 1.5 mb.

    I have attempted to change the chunk size to 512, 1024 and 4096 none of which improve the situation. I am fairly sure it is because of our slow transfer speed we currently have.

    Most likely I could solve the problem if I understood how to change the socket time out or if I could build in a loop that exits when the socket is not timing out.

    Any suggestions or sample code that could help me overcome this limitation would be appreciated. The company is unfortunately not going to upgrade their data transfer connections at this time so that is not an option.

    In the php manual I see the following code for adjusting the socket speed. However I am not sure how to intergrate the needed parts of this code into my code. I think I need the parts I have bolded.
    Code:
    <?php
    [B]$fp = fsockopen("www.example.com", 80);[/B]
    if (!$fp) {
        echo "Unable to open\n";
    } else {
    
        fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
        [B]stream_set_timeout($fp, 2);[/B]
        $res = fread($fp, 2000);
    
        $info = stream_get_meta_data($fp);
        fclose($fp);
    
    [B]//use some sort of loop instead here for testing if socket has timed out. then resume when socket is clear
        if ($info['timed_out'])[/B] {
            echo 'Connection timed out!';
        } else {
            echo $res;
        }
    
    }
    ?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    This kind of issue would more likely be with the HTTP server than PHP. The PHP code isn't really responsible for sending the data to the user. All it does is build the response, which it then hands of to the HTTP server to send. The PHP code should finish in milliseconds, even though it may take the HTTP server minutes to send the data to the client.

    I would look at the HTTP server configuration, to see if there are any timeouts set.


    Also, you may want to look at the readfile function. There is no need to open the file with fopen and then read and echo it in small chunks. The readfile function will take care of all that for you.

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Excellent answer. Just using the example for readfile solved the problem. We still have a crummy upload speed on the server, but at least until they decide to spend the bucks on improving that, the file transfer will work, albeit slow.

      Comment

      Working...