%PDF- %PDF-
| Direktori : /data/www_bck/varak.net_bck/wow2.varak.net/xml/ |
| Current File : //data/www_bck/varak.net_bck/wow2.varak.net/xml/getfile.php |
<?php
function download_file($file_source)
{
$ret = "";
if (function_exists('curl_init'))
{
// curl available
if (($ch = curl_init($file_source)) === FALSE) { return false; }
if (($fp = fopen("temp", 'wb')) === FALSE) { return false; }
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
// Begin transfer
if (!curl_exec($ch)) {
curl_close($ch);
fclose($fp);
return false;
};
curl_close($ch);
fclose($fp);
return file_get_contents("temp");
} else if (ini_get('allow_url_fopen') == 1) {
// curl unavailable, fopen is
$file_source = str_replace(' ', '%20', html_entity_decode($file_source)); // fix url format
//if (file_exists($file_target)) { chmod($file_target, 0777); } // add write permission
// Begin transfer
if (($rh = fopen($file_source, 'rb')) === FALSE) { return false; } // fopen() handles
//if (($wh = fopen($file_target, 'wb')) === FALSE) { return false; } // error messages.
while (!feof($rh))
{
// unable to write to file, possibly because the harddrive has filled up
/*if (fwrite($wh, fread($rh, 1024)) === FALSE) {
fclose($rh);
fclose($wh);
return false;
}*/
$ret .= fread($rh, 1024);
}
// Finished without errors
fclose($rh);
//fclose($wh);
return $ret;
} else {
// curl and fopen unavailable, try fsockopen
$url_array = parse_url($file_source);
$fp = fsockopen($url_array['host'], 80, $errno, $errstr, 5);
if (!fp) {
die("cURL isn't installed, 'allow_url_fopen' isn't set and socket opening failed. Socket failed because: <br /><br /> $errstr ($errno)");
} else {
//if (file_exists($file_target)) { chmod($file_target, 0777); } // add write permission
//if (($wh = fopen($file_target, 'wb')) === FALSE) { return false; }
// send the request for the file
$out = "GET " . $url_array[path] . "?" . $url_array[query] ." HTTP/1.0\r\n";
$out .= "Host: " . $url_array[host] . " \r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11\r\n";
//$out .= "Accept-Encoding: gzip\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
// Begin transfer
$gzip = false;
$head_end = false;
while (!feof($fp))
{
// unable to write to file, possibly because the harddrive has filled up
$tmp_w = fgets($fp, 2048);
if(trim($tmp_w) == "Content-Encoding: gzip")
{
$gzip = true;
}
if(!$head_end && trim($tmp_w) == "")
{
$head_end = true;
continue;
}
if($head_end)
{
/*if (fwrite($wh, $tmp_w) === FALSE)
{
fclose($fp);
fclose($wh);
return false;
}*/
$ret .= $tmp_w;
}
}
if($gzip)
{
$ret = gzinflate($ret);
}
// Finished without errors
fclose($fp);
//fclose($wh);
return $ret;
}
}
return false;
}
?>