Copying File (Server to Server) – wget.php

Here in Pakistan, For home users downloading/uploading speed is not comparable (very less) to the one we get on our LIVE hosting servers.
I had to install a PHP opensource solution LIVE, traditional process is that we download the archive on our PC from website, and then upload on our Server, which is definitely very much time consuming (downloading/uploading time) if your archive is 100MB+.

So, I wrote a little PHP script, which when executed on server, It will fetch the downloadable file content from source server and store locally on destination server (local). It is equivalent to the wget command we have in linux, if your hosting allow SSH access. If it is not like in my case … you can use wget.php wrapper. This can also be used to backup one server files to another.

Here is the sample code for file "wget.php"

<?php
error_reporting(E_ALL);
ini_set("display_errors","on");

$url = $_GET["url"];
$f = explode("/",$url);

$f = $f[count($f)-1];

function get_contents($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

$data = curl_exec($ch);
curl_close($ch);
return $data;
}

$d = get_contents($url);
if (file_put_contents($f,$d))
echo $f ." downloaded";
else
echo "Failed";

Advertisements