PHP download resume functionality - please help with code!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
szalinski
Forum Newbie
Posts: 14
Joined: Sat Oct 20, 2007 12:58 pm

PHP download resume functionality - please help with code!

Post by szalinski »

Please can somebody tell me why this isn't returning the index page? It is technically supposed to, and if somebody can get it to work I can then use remote download resume functionality in my site. According to http://www.answers.com/http, it is a HTTP 1.1 request that supposedly should work...I can't find any syntax errors! What's wrong?

Code: Select all

<?php
ob_end_clean();
ob_implicit_flush(TRUE);

$host = "www.rapidleech.com";
$url = "/index.html";
$port = 80;
$saveToFile = "index.html";
$nn = "\r\n";
$method = "GET";

$fp = @fsockopen($proxyHost ? $scheme.$proxyHost : $scheme.$host, $proxyPort ? $proxyPort : $port, $errno, $errstr, 15);

if (!$fp)
  {
  html_error("Couldn't connect to ".($proxyHost ? $proxyHost : $host)." at port ".($proxyPort ? $proxyPort : $port), 0);
  }

if($errno || $errstr)
  {
  $lastError = $errstr;
  print $errstr."<br>".$errno."<br>";
  }

socket_set_timeout($fp, 120);

if ($saveToFile)
	{
	if ($proxy)
		{
		echo "<p>Connected to proxy: <b>".$proxyHost."</b> at port <b>".$proxyPort."</b>...<br>\n";
		echo "GET: <b>".$host.$url."</b>...<br>\n";
		}
	else
		{
		echo "<p>Connected to: <b>".$host."</b> at port <b>".$port."</b>...<br>";
		}
	}

if ($saveToFile)
  {
  do {
  $redirect = "";
  
  $request =
  "HEAD ".str_replace(" ", "%20", $url)." HTTP/1.1".$nn.
  "Host: ".$host.$nn.
  "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MEGAUPLOAD 1.0; Alexa Toolbar)".$nn.
  "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5".$nn.
  "Accept-Language: en-en,en;q=0.5".$nn.
  "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7".$nn.
  "Pragma: no-cache".$nn.
  "Cache-Control: no-cache".$nn.
  "Keep-Alive: 300".$nn.
  "Connection: Keep-Alive".$nn.$nn;

  fwrite($fp, $request);
  
  //$fs = fopen("debug.txt", "wb");
  //fwrite($fs, $request);

  $head = "";

  while(!feof($fp))
    {
    $head.= fgets($fp, 128);
    }
  
  print "<br>HEAD: ".$head.$nn;
  
  if (stristr($head, "Location:"))
    {
    $redirect = trim(cut_str($head, "Location:", "\n"));
    $rUrl = parse_url($redirect);
    $scheme = $rUrl["scheme"] == "https" ? "ssl://" : "";
    $port = $rUrl["port"] ? $rUrl["port"] : ($scheme == "ssl://" ? 443 : 80);
    $host = $rUrl["host"];
    $url = $rUrl["path"].($rUrl["query"] ? "?".$rUrl["query"] : "");
    }
  
  if (stristr($head, "Content-Disposition:"))
		{
		$Content = trim(cut_str($head, "Content-Disposition:", "\n"))."\n";
		if (stristr($Content, "filename="))
      {
      $FileName = trim(trim(trim(cut_str($Content, "filename=", "\n")), ";"), '"');
			$saveToFile = dirname($saveToFile).PATH_SPLITTER.basename($FileName);
      }
		}
	/*
	if (@file_exists($saveToFile))
    {
    if (is_numeric($filesize = @filesize($saveToFile)))
      {
      $Resume["use"] = TRUE;
      $Resume["from"] = intval($filesize);
      }
    }
  */  
  } while (!empty($redirect));
  }



$request =
$method." ".str_replace(" ", "%20", $url)." HTTP/1.1".$nn.
"Host: ".$host.$nn.
"User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; MEGAUPLOAD 1.0; Alexa Toolbar)".$nn.
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5".$nn.
"Accept-Language: en-en,en;q=0.5".$nn.
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7".$nn.
"Pragma: no-cache".$nn.
"Cache-Control: no-cache".$nn.
($Resume["use"] === TRUE ? "Range: bytes=".$Resume["from"]."-".$nn : "").
$http_auth.
$proxyauth.
$referer.
$cookies.
"Keep-Alive: 300".$nn.
"Connection: Keep-Alive".$nn.
$content_tl.$nn.$postdata;

//fwrite($fs, $request);
//fclose($fs);

fwrite($fp, $request);


while(!feof($fp))
	{
 	$data.= fgets($fp, 128);
	}
	print "<br>GET: ".$data.$nn;
	
fclose($fp);


function cut_str($str, $left, $right)
  {
  $str = substr(stristr($str, $left), strlen($left));
  $leftLen = strlen(stristr($str, $right));
  $leftLen = $leftLen ? -($leftLen) : strlen($str);
  $str = substr($str, 0, $leftLen);
  return $str;
  }
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Can you tell us what part of all that code you think is not working?
(#10850)
szalinski
Forum Newbie
Posts: 14
Joined: Sat Oct 20, 2007 12:58 pm

Post by szalinski »

well that's just it, i don't know...it should download the index.html, but i'm not sure if i got the syntax (or more importantly, the structure) correct.
szalinski
Forum Newbie
Posts: 14
Joined: Sat Oct 20, 2007 12:58 pm

Post by szalinski »

i can't understand why the HEAD request times out before the GET request is executed...CAN SOMEBODY PLEASE HELP (i feel like i'm in a crowded room) :(
szalinski
Forum Newbie
Posts: 14
Joined: Sat Oct 20, 2007 12:58 pm

Post by szalinski »

has no-one got any idea what's wrong with this? :( :(
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

There's too much code for us to debug for you. Test it again and post exactly what it is doing, exactly what you want it to do and any error messages that you are receiving.
Post Reply