PHP: Descargar un fichero a la PC
Muchas veces es necesario descargar un determinado archivo sin que el navegador nos lo embeba con el maldito Windows Media Player o en caso de Ubuntu un plugins en Rhythmbox o Mplayer.
Para eso debemos agregar unas lineas en los headers de php…
download.php
$mainpath = $_GET["folder"]."/".$_GET["file"]; $file = $_GET["file"]; $filesize2 = sprintf("%u", filesize($mainpath)); #print "filesize is $filesize2"; if(!$fdl=@fopen($mainpath,'r')){ #include ("$header"); echo " <span class="\"changed\"">ERROR - Invalid Request (Downloadable file Missing or Unreadable)</span> "; die; }else{ set_time_limit(0); # Send the headers then send the file #------------------------------------ header("Cache-Control: ");# leave blank to avoid IE errors header("Pragma: ");# leave blank to avoid IE errors header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$file."\""); header("Content-length:".(string)($filesize2)); #header("Content-Length: ".$filesize2); #header("Content-Length: $filesize2"); sleep(1); fpassthru($fdl); }
Como verán utiliza variables GET y para usarlo debemos llamarlo así…
<a href="/download.php?folder=carpeta/carpeta2/carpeta3&file=cancion.mp3" target="_blank">Descargar</a>
Saludos…
Puedes seguir cualquier respuesta a esta entrada mediante el canal RSS 2.0. Puedes dejar un comentario o enviar un trackback desde tu propio sitio.














