Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/05/2006, 17:58
Computer XTress
 
Fecha de Ingreso: diciembre-2004
Mensajes: 721
Antigüedad: 19 años, 3 meses
Puntos: 2
Ayuda con script de despacho de downloads

Hola ¿Que tal?

Estove unas cuantas horas armando un script para despachar descargas (para tener cierto control sobre los downloads de archivos).

Bueno la verdad es que ya lo traigo BASTANTE pulido al tema, y solo me encuentro con un error... veran, en un principio el problema era que cuando el usuario cargaba el script pidiendo un archivo (supongamos: dl_dispatch.php?archivo=ukulele.mp3&tipo=audio-mp3), no podía éste visitar otras páginas ni iniciar otros downloads hasta que el primer download haya finalizado... lo resolvi agregando "session_write_close();" antes de iniciar la descarga... FUNCIONO, pero ahora me pasa lo mismo, exactamente el mismo problema, cuando el usuario esta descargando la cantidad máxima de descargas permitidas por su explorador.

¿Acaso alguno sabe como puedo resolver esto? ¿Será algun problema en los headers? (ya que con headers no estoy muy confiansudo aún).

Adjunto el script, resumido... si les es de utilidad el script por denle el uso que deseen.

SALUDOS.

Código PHP:
session_start();
session_write_close(); // (!)

$fsize filesize($fpath);
$bufsize 20000;

if(isset(
$_SERVER['HTTP_RANGE']))  // Descargas parciales (php.net)
{
   if(
preg_match("/^bytes=(\\d+)-(\\d*)$/"$_SERVER['HTTP_RANGE'], $matches)) { //parsing Range header
       
$from $matches[1];
       
$to $matches[2];
       if(empty(
$to))
       {
           
$to $fsize 1;  // restamos el último byte
       
}
       
$content_size $to $from 1;

       
header("HTTP/1.1 206 Partial Content");
       
header("Content-Range: $from-$to/$fsize");
       
header("Content-Length: $content_size");
       
header("Content-Type: application/force-download");
       
header("Content-Disposition: attachment; filename=$fname");
       
header("Content-Transfer-Encoding: binary");
       
header("Cache-Control: private");

       if(
file_exists($fpath) && $fh fopen($fpath"rb"))
       {
           
fseek($fh$from);
           
$cur_pos ftell($fh);
           while(
$cur_pos !== FALSE && ftell($fh) + $bufsize $to+1)
           {
               
$buffer fread($fh$bufsize);
               print 
$buffer;
               
$cur_pos ftell($fh);
           }

           
$buffer fread($fh$to+$cur_pos);
           print 
$buffer;

           
fclose($fh);
       }
       else
       {
           
header("HTTP/1.1 404 Not Found");
           exit;
       }
   }
   else
   {
       
header("HTTP/1.1 500 Internal Server Error");
       exit;
   }
}
else 
// Download normal
{
   
header("HTTP/1.1 200 OK");
   
header("Content-Length: $fsize");
   
header("Content-Type: application/force-download");
   
header("Content-Disposition: attachment; filename=$fname");
   
header("Content-Transfer-Encoding: binary");
   
header("Cache-Control: private");

   if(
file_exists($fpath) && $fh fopen($fpath"rb")){
       while(
$buf fread($fh$bufsize))
           print 
$buf;
       
fclose($fh);
   }
   else
   {
       
header("Location: /msgs/404.php");
   }