@dewadam;
<?php
error_reporting(E_ALL);
set_time_limit(0);
/**
* Copy remote file over HTTP one small chunk at a time.
*
* @param $in_file The full URL to the remote file
* @param $out_file The path where to save the file
*/
function copyfile_chunked($in_file = null, $out_file = null) {
$timer_start = microtime(true);
$chunk_size = 10 * (1024 * 1024); # 10 Mb
if(is_null($in_file) || strlen($in_file) <= 0)
return false;
if(is_null($out_file) || strlen($out_file) <= 0)
return false;
/**
* parse_url breaks a part a URL into it's parts, i.e. host, path,
* query string, etc.
*/
$parts = parse_url($in_file);
$i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
$o_handle = fopen(str_replace("\\", "/", realpath($out_file)), 'wb');
if($i_handle == false || $o_handle == false)
return false;
if(!empty($parts['query']))
$parts['path'] .= '?' . $parts['query'];
/**
* Send the request to the server for the file
*/
$request = "GET {$parts['path']} HTTP/1.1\r\n";
$request .= "Host: {$parts['host']}\r\n";
$request .= "User-Agent: Mozilla/5.0\r\n";
$request .= "Keep-Alive: 115\r\n";
$request .= "Connection: keep-alive\r\n\r\n";
fwrite($i_handle, $request);
/**
* Now read the headers from the remote server. We'll need
* to get the content length.
*/
$headers = array();
while(!feof($i_handle)) {
$line = fgets($i_handle);
if($line == "\r\n") break;
$headers[] = $line;
}
/**
* Look for the Content-Length header, and get the size
* of the remote file.
*/
$length = 0;
foreach($headers as $header) {
if (stripos($header, 'Content-Length:') === 0) {
$length = (int) str_replace('Content-Length: ', '', $header);
break;
}
}
/**
* Start reading in the remote file, and writing it to the
* local file one chunk at a time.
*/
$cnt = 0;
while(!feof($i_handle)) {
$buf = '';
$buf = fread($i_handle, $chunk_size);
$bytes = fwrite($o_handle, $buf);
if($bytes == false)
return false;
$cnt += $bytes;
/**
* We're done reading when we've reached the conent length
*/
if ($cnt >= $length) break;
}
fclose($i_handle);
fclose($o_handle);
$timer_stop = microtime(true);
return (object) array(
"status" => true,
"downloaded_byte" => $cnt,
"request" => (object) array(
"requested_at" => $timer_start,
"responded_at" => $timer_stop,
"elapsed_time" => ($timer_stop - $timer_start)
),
"file" => (object) array(
"location" => str_replace("\\", "/", realpath($out_file)),
"stat" => @stat(str_replace("\\", "/", realpath($out_file)))
)
);
}
print_r(
copyfile_chunked("http://tr.wordpress.org/wordpress-3.7.1-tr_TR.zip", "wordpress-3.7.1-tr_TR.zip")
);
/*
stdClass Object
(
[status] => 1
[downloaded_byte] => 5452282
[request] => stdClass Object
(
[requested_at] => 1384871051.4746
[responded_at] => 1384871066.7435
[elapsed_time] => 15.268930912018
)
[file] => stdClass Object
(
[location] => D:/XAMPP/htdocs/calisma.alani/wordpress-3.7.1-tr_TR.zip
[stat] => Array
(
[0] => 3
[1] => 0
[2] => 33206
[3] => 1
[4] => 0
[5] => 0
[6] => 3
[7] => 5452282
[8] => 1384870582
[9] => 1384871066
[10] => 1384870582
[11] => -1
[12] => -1
[dev] => 3
[ino] => 0
[mode] => 33206
[nlink] => 1
[uid] => 0
[gid] => 0
[rdev] => 3
[size] => 5452282
[atime] => 1384870582
[mtime] => 1384871066
[ctime] => 1384870582
[blksize] => -1
[blocks] => -1
)
)
)
*/