Archive for October, 2010

  • Como Descargar Un Archivo En PHP

    0

    Descargar un archivo en PHP implica leer un archivo de una locación en el servidor y escribir hacia el cliente. Hay varias opciones disponibles a la hora de la descarga.

    Primero localizamos y abrimos el archivo

    $path = "http://myserver/somelocation/somefile.pdf";
    $fd = fopen($path, "r")
    $fsize = filesize($path);
    $path_parts = pathinfo($path);
    $ext = strtolower($path_parts["extension"]);

    Escribimos los headers para la descarga

    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    header("Content-length: $fsize");

    Escribimos el archivo hacia el browser

    readfile($path);
    fclose($fd);
    exit;