Hey guys. I need to download a file to my server given a URL but it isn't working. For some reason it isn't downloading the whole file, it is only getting part. Here is my code:

<?php 

/* Tutorial by AwesomePHP.com -> www.AwesomePHP.com */ 
/* Function: download remote file */ 
/* Parameters: $url -> to download | $dir -> where to store file | 
    $file_name -> store file as this name - if null, use default*/ 

function downloadRemoteFile($url,$dir,$file_name = NULL){ 
    if($file_name == NULL){ $file_name = basename($url);} 
    $url_stuff = parse_url($url); 
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80; 

$fp = fsockopen($url_stuff['host'], $port); 
if(!$fp){ return false;} 

$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n"; 
$query .= 'Host: ' . $url_stuff['host']; 
$query .= "\n\n"; 

fwrite($fp, $query); 

while ($tmp = fread($fp, 8192))   { 
    $buffer .= $tmp; 
} 

preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts); 
$file_binary = substr($buffer, - $parts[1]); 
if($file_name == NULL){ 
    $temp = explode(".",$url); 
    $file_name = $temp[count($temp)-1]; 
} 
$file_open = fopen($dir . "/" . $file_name,'w'); 
if(!$file_open){ return false;} 
fwrite($file_open,$file_binary); 
fclose($file_open); 
return true; 
}  
?>

I found it from Here

Like I said, I am only getting about 4 Kb of the files I am testing with:
Test Link 1
Test Link 2
Test Link 3
Test Link 4
Test Link 5

Thanks in advance!

    I'd start by replacing this:

    function downloadRemoteFile($url,$dir,$file_name = NULL){ 
        if($file_name == NULL){ $file_name = basename($url);} 
        $url_stuff = parse_url($url); 
        $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80; 
    
    $fp = fsockopen($url_stuff['host'], $port); 
    if(!$fp){ return false;} 
    
    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n"; 
    $query .= 'Host: ' . $url_stuff['host']; 
    $query .= "\n\n"; 
    
    fwrite($fp, $query); 
    
    while ($tmp = fread($fp, 8192))   { 
        $buffer .= $tmp; 
    } 
    
    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts); 
    $file_binary = substr($buffer, - $parts[1]); 
    if($file_name == NULL){ 
        $temp = explode(".",$url); 
        $file_name = $temp[count($temp)-1]; 
    } 
    $file_open = fopen($dir . "/" . $file_name,'w'); 
    if(!$file_open){ return false;} 
    fwrite($file_open,$file_binary); 
    fclose($file_open); 
    return true; 
    }

    with this:

    function downloadRemoteFile($url,$dir,$file_name = NULL){ 
        return (bool)file_put_contents("$dir/$file_name", file_get_contents($url));
    } 
      bradgrafelman;10982226 wrote:

      I'd start by replacing this:

      ...

      with this:

      function downloadRemoteFile($url,$dir,$file_name = NULL){ 
          return (bool)file_put_contents("$dir/$file_name", file_get_contents($url));
      } 

      I tried that but it still looks like I'm only getting about 4 KB of a 139 KB file. Is there any way to test why it isn't getting the whole file? Like can I somehow get errors if there are any?

      Thanks for the help

        Do you have the PHP directives display_errors set to On and error_reporting set to E_ALL?

          bradgrafelman;10982228 wrote:

          Do you have the PHP directives display_errors set to On and error_reporting set to E_ALL?

          Yes, I do. But I don't see anything.

          EDIT: The file I want to download is an apk (android package file) if that matters.

            It looks like it is not working for different types of files either. I tried it for an image (.jpg) and it still didn't work. Is there anything in my php.ini which may be blocking it?

            EDIT:
            I also tried:

                if(!@copy($_url,$_dir))
            	{
                	$errors= error_get_last();
                	echo "COPY ERROR: ".$errors['type'];
                	echo "<br />\n".$errors['message'];
            	} else {
                	echo "File copied from remote!";
            	}

            But I got the following error:

            copy(): failed to open stream: HTTP request failed!
              cagagnon;10982232 wrote:

              copy(): failed to open stream: HTTP request failed!

              This is all of the error message? Does it not tell you the reason why it failed? perhaps something like

              copy(http://example.com/no_such_file.ext) [function.copy]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
              
                Write a Reply...