Jump to content

Recommended Posts

I had an idea of Adding Multi-threading functionality to php ( i called it PHPThreader ) in little words the idea was :-

1. make a Js class which Creates Ajax Objects in order to call PHP Scripts(Threads)

2. then make a PHP class Implements and Interfaces the Js Class

3. Implement the PHP Class with your script easily and LAUNCH threads very easy Just Like:-

$obj=new Thread($filename,$Inputs); in other words:-

$e=new Thread("Download.php","url="http://www.phpfreaks.com");

 

you will find the Classes & Documentation & Examples at :-

where

 

AjT.php        : the Js Class which is implemented by the PHP class

 

Threader.php : PHP Class, which is a front to deal with the Js Class

 

Bag.php        : is also dealing with Js Class to know the status of Thread( a finishing Alarm )

 

Please tell me what do u think of the Idea & Implementation .Thanks in advance

Heeeeey Guys what's wrong ? >:( >:(

23 reads 18 hours No responses No trails/downloads ( I checked SF )

 

the Link again:-

http://sourceforge.net/project/showfiles.php?group_id=203377&package_id=242365&release_id=532251

 

Please Try again and give me ur feedbacks i need my class reviewed.

Seems like an interesting idea. I can't test it out but from the code it seems simple enough and functional.

 

I think it would be quite useful in certain applications where a script loads data from multiple locations.

 

I read some of your code and I can see it will not work with IE5 and IE6 due to it use the xmlhttprequest() method. I understand you are not too concerned about Internet Explorer 6 but it is widely used and should be taken into account for a public project like this. Simply do something similar to below to use ActiveX.

 

try{
obj = new XMLHttpRequest();
}catch(e if (!obj)){
obj = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
obj = new ActiveXObject("Microsoft.XMLHTTP");
}

Thanks alot for your concern

Internet Explorer 6 but it is widely used and should be taken into account for a public project like this

Ok this is a point i will add it

I can't test it

i think u will like trying the second example it is a PDM(Php Download Manager) which has the abillity to open a number of connections at the same time and display a simple progress of each connection

loads data from multiple locations

i was thinking of an RSS reader example that graps them from multiple resources at the same time which saves time and makes the best use of the internet connection . But i didn't find good examples for reading RSS feads using PHP  ??? 

 

UP

this is not testing this is ignorance. But if downloading is that hard here is the code

AjT.php

<?php
/*Author :ASDen(Mohmed Yousef)
  Email  :harrrrpo@gmail.com
  Email me for any help or just encourage me and tell me it does work !
  the Js class which implements Ajax*/ 
echo"
<script language=\"JavaScript\">
var i=0;
function Threader(url,num,parameters)
{
this.request = new XMLHttpRequest();
this.request.open(\"POST\", url, true);
this.request.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");
this.request.setRequestHeader(\"Content-length\", parameters.length);
this.request.setRequestHeader(\"Connection\", \"close\");
this.params=parameters;
this.Tnum=num;
this.request.onreadystatechange=ChangeBag;
this.begin=Starton;
i=i+1;
}
function Starton()
{this.request.send(this.params);}
function ChangeBag()
{
// all requests go here so it's our bag and here telling bag script if a thread finished
for (var x=0;x<i;x++)
{
if (t[x].request.readyState == 4 && t[x].Tnum!=-1)
{var r2=new XMLHttpRequest();
r2.open(\"GET\", \"Bag.php?num=\"+t[x].Tnum, true);
r2.send(null);
t[x].Tnum=-1;}
}
}
var t=new Array();
</script>
";
?>

 

Threader.php

<?php
/*Author :ASDen(Mohmed Yousef)
  Email  :harrrrpo@gmail.com
  Email me for any help or just encourage me and tell me it does work !
  The PHP Class implementing the Js Class */ 
require("AjT.php");
class Thread
{
var $pos;
var $statevar;
function __construct($url,$params)
  {
  	$this->statevar=0;
  static $num=-1;
  $num++;
  $this->pos=$num;
  //new instance of the Js class
  echo "<script language=\"javascript\">";
  echo "t[$num]=new Threader(\"$url\",$num,\"$params\");";
  echo "</script>";
  }
  function Go()
  {
  	// fire on
  echo "<script language=\"javascript\">";
  echo "t[$this->pos].begin();";
  echo "</script>";
  flush();
  }
  function State()
  {
  	if($this->statevar==0){$this->statevar=file_exists($this->pos);}
  	if(file_exists($this->pos)==1){unlink($this->pos);	}
  	return $this->statevar ;  
   // return file_exists($this->pos);
  
  }
  
  function  alert($t)
  {
  	//just a small alerting in php irrelevant but i like it
  echo "<script language=\"javascript\">";
  echo "alert($t);";
  echo "</script>";
  flush();  	
  }
}

?>

 

Bag.php

<form name="asd" action="Bag.php" method="GET">
<input type="text" name="num">
</form>
<?php
/*Author :ASDen(Mohmed Yousef)
  Email  :harrrrpo@gmail.com
  Email me for any help or just encourage me and tell me it does work !
  here is invoked when a thread has finished and we creat a file to show this */
$f=fopen($_GET["num"],'wb');
?>

 

the PDM example

 

PDM.php

<?php
/*Author :ASDen(Mohmed Yousef)
  Email  :harrrrpo@gmail.com
  Email me for any help or just encourage me and tell me it does work !
  Example 2 main script (a download manager)*/
require("Threader.php");

ini_set('max_input_time','0'); 
ini_set('max_execution_time','0'); 
ini_set('max_input_time','0'); 
//the file to be downloaded url
$url="http://belnet.dl.sourceforge.net/sourceforge/sevenzip/7z442.exe"; 
//number of connections
$num=5;
$ss=remote_file_size($url);
$part=floor($ss/$num);

echo "<html><body><form name=\"asd\">";
for($i=0;$i<$num;$i++)
{
echo "Part$i <input type=\"text\" name=\"a$i\"><br>";
}
echo "</form></body>";flush(); 
$i=0;
for($i=0;$i<$num-1;$i++)
{
	//initialize threads
$e[$i]=new Thread("PartDownloader.php","Fname=$url&FFname=asd$i.asd&Range=".($i*$part)."-".(($i+1)*$part-1));
$e[$i]->Go();
}
$e[$num-1]=new Thread("PartDownloader.php","Fname=$url&FFname=asd$i.asd&Range=".(($num-1)*$part)."-".(1*$ss));
$e[$num-1]->Go();
//monitor the status of downloading
while (all($e,$num))  
   {
   	sleep(0.5);
   	echo "<script language=\"javascript\">";
   	for($i=0;$i<$num;$i++)
    {
    echo "document.asd.a$i.value="."\"".(round(remote_file_size("http://localhost/asd$i.asd")/$part*100,2))."%\";";
    }
    echo "</script>";
  	flush();
   }
   $ww=fopen("asd.exe","wb");
   fclose($ww);
   $ww=fopen("asd.exe",'a');
   for($i=0;$i<$num;$i++)
{
	$wh=fopen("asd$i.asd",'r');
	fwrite($ww,fread($wh,filesize("asd$i.asd")));
	fclose($wh);
	unlink("asd$i.asd");
}
   
function all($e,$num) 
{

	for($i=0;$i<$num;$i++)
	{if($e[$i]->State()==0){return 1;}}
	return 0;
}

   //a big function but  yet very helpfull to get file size as filesize function doesnot work properly
   function remote_file_size ($url) 
{
   $head = "";
   $url_p = parse_url($url);
   $host = $url_p["host"];
   $path = $url_p["path"];

   $fp = fsockopen($host, 80, $errno, $errstr, 20);
   if(!$fp) 
   { return false; } 
   else 
   {
       fputs($fp, "HEAD ".$url." HTTP/1.1\r\n");
       fputs($fp, "HOST: dummy\r\n");
       fputs($fp, "Connection: close\r\n\r\n");
       $headers = "";
       while (!feof($fp)) {
           $headers .= fgets ($fp, 128);
       }
   }
   fclose ($fp);
   $return = false;
   $arr_headers = explode("\n", $headers);
   foreach($arr_headers as $header) {
       $s = "Content-Length: ";
       if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
           $return = substr($header, strlen($s));
           break;
       }
   } 
   return $return;
}  
?>

PartDownloader.php

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
URL:<input type="text" name="Fname">
name:<input type="text" name="FFname">
Range:<input type="text" name="Range">
<input type="submit" value="Down&Mail" name="submit">
</form>
<?php  
/*Author :ASDen(Mohmed Yousef)
  Email  :harrrrpo@gmail.com
  Email me for any help or just encourage me and tell me it does work !
  Example 2 thread  more complicated downloads of parts */

$url=trim($_POST["Fname"]);
$range=$_POST["Range"];
$wh = fopen($url, 'wb');
   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL,$url);

   curl_setopt($ch, CURLOPT_VERBOSE, 1);

   curl_setopt($ch, CURLOPT_POST, 0);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch,CURLOPT_RANGE,$range);
$file_target=$_POST["FFname"];
   $wh = fopen($file_target, 'wb');
curl_setopt($ch, CURLOPT_FILE, $wh); 
$dataq = curl_exec($ch);
curl_close ($ch);
fclose($wh);
?>
</body>
</html>

 

 

 

 

Hh it's ur JOB now I'm waiting

  • 11 months later...

Hello everybody  ;D

it's been around 9 months since the last update to class and finally i got time to it

PHPThreader 1.6 Released

you can get it from sf here : http://sourceforge.net/project/showfile ... _id=203377

or from phpclasses here : http://www.phpclasses.org/browse/package/4082.html

---

The new version has the following features :-

1)Full Online Documentation of class here : http://ajphpth.sourceforge.net [An offline version is provided in sf]

2)new Communication Class : Shared Memory (shmop required )

3)new Thread Calling Class : CliThread (using shell calls)

4)new Abstract Mutex Class which can be used from user defined Communication Clases .

5)new automatic System of thread watching

6) a simple loader to overcome path issues

7)new methods like Join()

---

hope to hear from you about it

Thanks

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.