So I am trying to transfer/copy a large file from a remote server to my server in separate parts or chunks. I tried the script provided here: https://stackoverflow.com/a/4000569/7559794 I made some changes and gave a form to the script and used AJAX to communicate with PHP script as follows: The index.html contains a form with POST method and a submit button that calls the following function OnClick:
<script>
function stratdown() {
var partsizevalue = $('#psize').val();
var dfilename = $('#dfile').val();
var sfilename = $('#sfile').val();
$.ajax({
type: "POST",
url: 'trigger.php',
data:{dfile: dfilename, sfile: sfilename, psize: partsizevalue}, //{value: a, cID: cID}
success:function(result) {
alert(result);
}
});
}</script>
the file trigger.php contains the following script:
<?php
require 'partdown.php';
$infile = $_POST["dfile"];
$outfile = $_POST["sfile"];
$partsize = $_POST["psize"];
echo $mesg = DownInParts::copyfile_chunked($infile, $outfile, $partsize);
?>
and the required partdown.php contains:
<?php
/**
* Copy remote file over HTTP one small chunk at a time.
*
* @param $infile The full URL to the remote file
* @param $outfile The path where to save the file
*/
class DownInParts
{
public function copyfile_chunked($infile, $outfile, $partsize)
{
$chunksize = $partsize * 1024; // 10 Megs
/**
* parse_url breaks a part a URL into it's parts, i.e. host, path,
* query string, etc.
*/
$parts = parse_url($infile);
$i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
$o_handle = fopen($outfile, '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, $chunksize);
$bytes = fwrite($o_handle, $buf);
if ($bytes == false) {
return false;
}
$cnt += $bytes;
/**
* We're done reading when we've reached the content length
*/
if ($cnt >= $length) break;
}
fclose($i_handle);
fclose($o_handle);
return $cnt;
}
}
?>
I didn't manage to make it function. I have however tried to add the action "trigger.php" to the form in order to call PHP directly yet The file I downloaded was (0 Kb) in size. Any ideas.