Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: upload file status / progress bar z ajaxem
Forum PHP.pl > Forum > PHP
bartek24m
Witam mam skrypt u siebie na serwerze który odpowiada za wysylanie plików na rapidshare

skrypt działa tak ...

zaznaczam plik do wyslania i klikam w formularzu submit aby mogl odpalić się skrypt do uploadowania plików

  1. <?php
  2. <input type="submit" target="_blank" name="upload1" style="FONT-SIZE: 10px;border:1px solid #000000;"  title="Zaznacz Pliki i kliknij ten przycisk aby wgrać na serwer rapidshare.com !" value="+ Upload na Rapidshare" />&nbsp;
  3. ?>



  1. <?php
  2. if(isset($_POST['upload1'])){
  3. $c=count($_POST['zaznacz']);
  4. //echo $c;
  5. echo ('<center>Pomyślnie zostały wgrane pliki poniżej linki do tych plików !<BR><form><TEXTAREA style="FONT-SIZE: 11px;border:1px dashed #FF00FF;" rows="8" cols="90" name="pole">');
  6. for ($x=1; $x<$c+1; $x++)
  7. {
  8.  //echo $_POST['zaznacz'][$x-1];
  9.  $upload_file = $_POST['zaznacz'][$x-1];
  10.  $upload=new rapidphp;
  11.  $upload->config("prem","login","pass");
  12.  $wynik=$upload->sendfile("$upload_file");
  13.  echo ($wynik[0]);
  14.  echo "\n";
  15. }
  16. echo ('</TEXTAREA></form></center>');
  17. }
  18. ?>


oczywiście wszystko działa dzięki tej klasie

  1. <?php
  2. class rapidphp {
  3.  
  4.         private $maxbuf=64000; // max bytes/packet
  5.         private $uploadpath="l3";
  6.         private $zone,$login,$passwort;
  7.  
  8.         private function hashfile($filename) { // md5 hash of files
  9.             return strtoupper(md5_file($filename));
  10.         }
  11.         public function getserver() { // gets server for upload
  12.             while(empty($server)) {
  13.                  $server=file_get_contents("http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1");
  14.          }
  15.                 return sprintf("rs%s%s.rapidshare.com",$server,$this->uploadpath);
  16.         }
  17.         public function config($zone,$login="",$passwort="") { // configuration
  18.             $this->zone=$zone;
  19.                 $this->login=$login;
  20.                 $this->passwort=$passwort;
  21.         }
  22.         public function sendfile($file) { // upload a file
  23.             if(empty($this->zone)) {
  24.                     $this->zone="free";
  25.                 }
  26.                 if($this->zone=="prem" OR $this->zone=="col") {
  27.                     if(empty($this->login) OR empty($this->passwort)) {
  28.                             $this->zone="free";
  29.                         }
  30.                 }
  31.                 if(!file_exists($file)) {
  32.                     die("File not found!");
  33.                 }
  34.                 $hash=$this->hashfile($file); // hash of the file
  35.                 $size=filesize($file); // filesize (bytes) of the file
  36.                 $cursize=0; // later needed
  37.                 $server=$this->getserver(); // get server for uploading
  38.              $sock=    fsockopen($server,80,$errorno,$errormsg,30) or die("Unable to open connection to rapidshare\nError $errorno ($errormsg)");
  39.        stream_set_timeout($sock,3600); // anti timeout
  40.                 $fp=    fopen($file,"r");
  41.                 $boundary = "---------------------632865735RS4EVER5675864";
  42.                 $contentheader="r\nContent-Disposition: form-data; name=\"rsapi_v1\"r\nr\n1r\n";
  43.                 if($this->zone=="prem") {  // premium
  44.              $contentheader .= sprintf("%sr\nContent-Disposition: form-data; name=\"login\"r\nr\n%sr\n",$boundary,$this->login);
  45.              $contentheader .= sprintf("%sr\nContent-Disposition: form-data; name=\"password\"r\nr\n%sr\n",$boundary,$this->passwort);
  46.                 }
  47.                 if($this->zone=="col") { // collector
  48.              $contentheader .= sprintf("%sr\nContent-Disposition: form-data; name=\"freeaccountid\"r\nr\n%sr\n",$boundary,$this->login);
  49.              $contentheader .= sprintf("%sr\nContent-Disposition: form-data; name=\"password\"r\nr\n%sr\n",$boundary,$this->passwort);
  50.                 }
  51.                 $contentheader .= sprintf("%sr\nContent-Disposition: form-data; name=\"filecontent\"; filename=\"%s\"r\nr\n",$boundary,$file);
  52.        $contenttail = "r\n".$boundary."--r\n";
  53.        $contentlength = strlen($contentheader) + $size + strlen($contenttail);
  54.                 $header = "POST /cgi-bin/upload.cgi HTTP/1.0r\nContent-Type: multipart/form-data; boundary=".$boundary."r\nContent-Length: ".$contentlength."r\nr\n";
  55.                 fwrite($sock,$header.$contentheader);
  56.                 while($cursize < $size) { // If we didn't upload everything, repeat!
  57.                     $buf=fread($fp,$this->maxbuf) or die("Unable to read file"); // read max bytes from the file
  58.                         $cursize=$cursize+strlen($buf);
  59.                         if(fwrite($sock,$buf)) { // send data
  60.                         }
  61.                 }
  62.                 fwrite($sock,$contenttail); // finished
  63.                 $ret=fread($sock,10000); // receive data (links, hash, bytes)
  64.                 preg_match("/r\nr\n(.+)/s",$ret,$match); // we don't need the http-header
  65.          $ret=explode("\n",$match[1]); // every line gets an entry in an array
  66.         fclose($sock);
  67.         fclose($fp);
  68.                 foreach($ret as $id => $cont) {
  69.                     if($id!=0) { // very boring stuff!
  70.                             if($id>4) break; // break foreach
  71.                             $key_val[]=substr($cont,8); // throw away the first eight chars
  72.                         }
  73.                 }
  74.                 if($hash==$key_val[3]) { // if the hash is == hash of the local file
  75.                  return $key_val;
  76.                 } else {  // omg! upload failed!
  77.                     printf("Upload FAILED! Your hash is %s, while the uploaded file has the hash %s",$hash,$key_val[3]);
  78.                         return FALSE;
  79.                 }
  80.         }
  81. }
  82. ?>



Jak zrobić aby po kliknięciu w formularz wyślij pojawila sie informacja z ajaxem z jaką prędkością uploaduje pliki
+ ile już % już uploadowało

obecnie wygląda to tak ze klikam na submit i czekam kilka chwil az wszystko się wyśle i wyświetli wynik

załóżmy że jestem wstanie wyciągnąć informacje o wielkości pliku, problem w tym ze nie wiem jak live wyświetlić z jaka prędkością są uploadowane pliki
czy jest ktoś kto mógłby mnie naprowadzić aby uzyskac tego typu informacje

erix
Cytat
problem w tym ze nie wiem jak live wyświetlić z jaka prędkością są uploadowane pliki

flush" title="Zobacz w manualu PHP" target="_manual + coś przesuwające progress bar.

Cytat
problem w tym ze nie wiem jak live wyświetlić z jaka prędkością są uploadowane pliki

Z tego, co pamiętam, to takie rzeczy tylko w Perlu. PHP nie ma odpowiednich funkcji.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.