Hello Everyone,
I do use a remote download php script for fetching files which should be backuped.
code 1: remote download
code 2: split function
code 3: cURL -range
Is it possible to add a file split function? so that I am
able to download the remote file as a splitted one that can be
merged to the original file in the end?
I found that cURL can split a file and download simultaneously from multiple locations... right now I do not know how to combine
both functions!!
code1:
code2:
code3:
Step One
Identify the download mirrors from the Ubuntu website. Choose at least 4 different mirrors. I have chosen the following locations :
url1=http://ftp.daum.net/ubuntu-releases/intrepid/ubuntu-8.10-desktop-i386.iso
url2=http://ftp.yz.yamagata-u.ac.jp/pub/linux/ubuntu/releases/intrepid/ubuntu-8.10-desktop-i386.iso
url3=http://ie.releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
url4=http://releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
Where url1, url2, url3 and url4 are variables which are associated with the corresponding full paths of the Ubuntu ISO file.
Step Two
We are going to split the file ubuntu-8.10-desktop-i386.iso into 4 parts, with 3 parts of 200 MB and the rest making up the 4th part. So the cURL command(s) will be as follows :
curl --range 0-199999999 -o ubuntu-iso.part1 $url1 &
curl --range 200000000-399999999 -o ubuntu-iso.part2 $url2 &
curl --range 400000000-599999999 -o ubuntu-iso.part3 $url3 &
curl --range 600000000- -o ubuntu-iso.part4 $url4 &
--range option retrieves a byte range in the first three cases 199.99 MB of the file. -o specifies the output file name and $url[1..4] are the above mentioned URLs.
For easy execution, I have created a bash script named “download.sh“, comprising of the above lines and to start the download, I need just run the script.
#FILE NAME : download.sh (Download accelerator)
#!/bin/sh
url1=http://ftp.daum.net/ubuntu-releases/intrepid/ubuntu-8.10-desktop-i386.iso
url2=http://ftp.yz.yamagata-u.ac.jp/pub/linux/ubuntu/releases/intrepid/ubuntu-8.10-desktop-i386.iso
url3=http://ie.releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
url4=http://releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
curl --range 0-199999999 -o ubuntu-iso.part1 $url1 &
curl --range 200000000-399999999 -o ubuntu-iso.part2 $url2 &
curl --range 400000000-599999999 -o ubuntu-iso.part3 $url3 &
curl --range 600000000- -o ubuntu-iso.part4 $url4 &
I do use a remote download php script for fetching files which should be backuped.
code 1: remote download
code 2: split function
code 3: cURL -range
Is it possible to add a file split function? so that I am
able to download the remote file as a splitted one that can be
merged to the original file in the end?
I found that cURL can split a file and download simultaneously from multiple locations... right now I do not know how to combine
both functions!!
code1:
PHP-Code:
<?php
function human_file_size($size)
{
$filesizename = array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i];
}
function duration($int_seconds=0, $if_reached=null)
{
$key_suffix = 's'; $periods = array('year'=>31556926,'month'=>2629743,'day'=>86400,'hour'=>3600,'minute'=>60,'second'=>1);
$flag_hide_zero = true;
foreach( $periods as $key => $length )
{
$temp = floor( $int_seconds / $length );
if( !$flag_hide_zero || $temp > 0 )
{
$build[] = $temp.' '.$key.($temp!=1?'s':null);
$flag_hide_zero = false;
}
$int_seconds = fmod($int_seconds, $length);
}
return ( !empty($build)?implode(', ', $build):$if_reached );
}
if(!is_writeable('./'))
{
exit('Cannot write to local dir (needs chmod).');
}
$remote_file = $_POST['url'];
if($_POST['output'])
{
$local_file = $_POST['output'];
}
else
{
$decoded_url = rawurldecode($remote_file);
$local_file = substr($decoded_url, strrpos($decoded_url, '/') + 1);
}
if(file_exists($local_file))
{
unlink($local_file);
echo("<b>$local_file</b> has Been Replaced<br>");
}
if($remote_file)
{
$startTime = time();
@set_time_limit(0);
ignore_user_abort();
$file_in = fopen($remote_file, 'r');
$file_out = fopen('./'.$local_file, 'a');
if($file_in)
{
while(!feof($file_in))
{
$buffer = fread($file_in, 8192);
fwrite($file_out, $buffer, 8192);
}
fclose($file_in);
fclose($file_out);
$size = filesize($local_file);
$humansize = human_file_size($size);
$seconds = time() - $startTime;
if($size <= 10240)
{
unlink($local_file);
exit("<b>$local_file</b> has been deleted");
}
$count = duration($seconds);
$url = rawurlencode($local_file);
exit("<b>Filename: </b><a href=$url>$local_file</a><br><b>Filesize: </b>$humansize<br><b>Time: </b>$count");
}
}
exit;
?>
PHP-Code:
<?php
// ---------------------------
// Edit variables (3 variables)
// ---------------------------
// File to split, is its not in the same folder with filesplit.php,
// full path is required. $filename = "http://www.remoteserver.com/path/tofile";
// Target folder. Splitted files will be stored here. Original file never gets touched.
// Do not append slash! Make sure webserver has write permission on this folder. $targetfolder = 'www.myserver.com/path/tofile';
// File size in Mb per piece/split.
// For a 200Mb file if piecesize=10 it will create twenty 10Mb files $piecesize = 150;
// splitted file size in MB
// ---------------------------
// Do NOT edit this section
// ---------------------------
$buffer = 1024;
$piece = 1048576*$piecesize; $current = 0;
$splitnum = 1;
if(!file_exists($targetfolder))
{
if(mkdir($targetfolder))
{
echo "Created target folder $targetfolder".br();
}
}
if(!$handle = fopen($filename, "rb"))
{
die("Unable to open $filename for read! Make sure you edited filesplit.php correctly!".br());
}
$base_filename = basename($filename);
$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
if(!$fw = fopen($piece_name,"w"))
{
die("Unable to open $piece_name for write. Make sure target folder is writeable.".br());
}
echo "Splitting $base_filename into $piecesize Mb files ".br()."(last piece may be smaller in size)".br();
echo "Writing $piece_name...".br();
while (!feof($handle) and $splitnum < 999)
{
if($current < $piece)
{
if($content = fread($handle, $buffer))
{
if(fwrite($fw, $content))
{
$current += $buffer;
}
else
{
die("filesplit.php is unable to write to target folder. Target folder may not have write permission! Try chmod +w target_folder".br());
}
}
}
else
{
fclose($fw);
$current = 0;
$splitnum++;
$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
echo "Writing $piece_name...".br();
$fw = fopen($piece_name,"w");
}
}
fclose($fw);
fclose($handle);
echo "Done! ".br();
exit;
function br()
{
return (!empty($_SERVER['SERVER_SOFTWARE']))?'<br>':"\n";
}
?>
Step One
Identify the download mirrors from the Ubuntu website. Choose at least 4 different mirrors. I have chosen the following locations :
url1=http://ftp.daum.net/ubuntu-releases/intrepid/ubuntu-8.10-desktop-i386.iso
url2=http://ftp.yz.yamagata-u.ac.jp/pub/linux/ubuntu/releases/intrepid/ubuntu-8.10-desktop-i386.iso
url3=http://ie.releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
url4=http://releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
Where url1, url2, url3 and url4 are variables which are associated with the corresponding full paths of the Ubuntu ISO file.
Step Two
We are going to split the file ubuntu-8.10-desktop-i386.iso into 4 parts, with 3 parts of 200 MB and the rest making up the 4th part. So the cURL command(s) will be as follows :
curl --range 0-199999999 -o ubuntu-iso.part1 $url1 &
curl --range 200000000-399999999 -o ubuntu-iso.part2 $url2 &
curl --range 400000000-599999999 -o ubuntu-iso.part3 $url3 &
curl --range 600000000- -o ubuntu-iso.part4 $url4 &
--range option retrieves a byte range in the first three cases 199.99 MB of the file. -o specifies the output file name and $url[1..4] are the above mentioned URLs.
For easy execution, I have created a bash script named “download.sh“, comprising of the above lines and to start the download, I need just run the script.
#FILE NAME : download.sh (Download accelerator)
#!/bin/sh
url1=http://ftp.daum.net/ubuntu-releases/intrepid/ubuntu-8.10-desktop-i386.iso
url2=http://ftp.yz.yamagata-u.ac.jp/pub/linux/ubuntu/releases/intrepid/ubuntu-8.10-desktop-i386.iso
url3=http://ie.releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
url4=http://releases.ubuntu.com/intrepid/ubuntu-8.10-desktop-i386.iso
curl --range 0-199999999 -o ubuntu-iso.part1 $url1 &
curl --range 200000000-399999999 -o ubuntu-iso.part2 $url2 &
curl --range 400000000-599999999 -o ubuntu-iso.part3 $url3 &
curl --range 600000000- -o ubuntu-iso.part4 $url4 &
EDIT:
line breaks sponsored by kropff