Re: Crear pagina de descargas de archivos (SOLUCIONADO)

From: Date: Fri, 21 Oct 2005 19:10:30 +0000
Subject: Re: Crear pagina de descargas de archivos (SOLUCIONADO)
References: 1 2 3  Groups: php.general.es 
Request: Send a blank email to general-es+get-56298@lists.php.net to get a copy of this message
Si necesitas _forzar_ la descarga, un redireccionamiento al archivo no es lo que necesitas, en esos casos depende del navegador si lo descarga o lo muestra embebido. Para lo que comentas yo utilizo esta función (si mal no recuerdo, ya resuelve algunos bugs de Explorer): /** * Forza el envío de un archivo. Indica si hubo éxito (TRUE / FALSE) *
 * @param  string $ruta_archivo            Ruta (real) al arhivo
* @param string $nombre_archivo_usuario Nombre que verá el usuario
 * @param  string $mime                    Tipo mime para la descarga
* @return boolean * @author rsalazar */ function force_send_file( $ruta_archivo, $nombre_archivo_usuario = '',
                          $mime = 'application/x-octet-stream' ) {
    // Verificaciones
    if ( !file_exists($ruta_archivo) || !is_readable($ruta_archivo)
         || headers_sent() ) {
        $str_debug = 'Fallo al intentar enviar archivo como descarga';
        if ( headers_sent() ) {
            $str_debug .= "\n- Cabeceras HTTP ya enviadas";
        }
        if ( !file_exists($ruta_archivo) ) {
            $str_debug .= "\n- Nombre de archivo o ruta errónea";
        }
        if ( !is_readable($ruta_archivo) ) {
            $str_debug .= "\n- No se tiene acceso (lectura) al archivo";
        }
        echo  $str_debug;
        return  FALSE;
    }
    // Asignación de los valores por defecto a los parámetros opcionales
    if ( empty($nombre_archivo_usuario) ) {
        $nombre_archivo_usuario = basename($ruta_archivo);
    }
    // Proceso de envío del archivo
    $file_len = filesize($ruta_archivo);
    $file_ts  = filemtime($ruta_archivo);
    $time_gmt = gmdate('D, d M Y H:i:s', $file_ts);
    if ( !empty($mime) ) {
        header("Content-Type: $mime");
    }
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Last-Modified: $time_gmt GMT");
    header("Content-Disposition: attachment; filename=\"$nombre_archivo_usuario\"");
    header("Content-Length: $file_len");
    readfile($ruta_archivo);
    return  TRUE;
} Rafael Morales wrote:
La solucion era batante simple, no se porque aveces lo sencillo se complica, todo lo que hice fue un simple: header("Location: https://servidor/ruta_al_archivo/archivo.xls"); -- Atentamente, J. Rafael Salazar Magaña Innox - Innovación Inteligente Tel: +52 (33) 3615 5348 ext. 205 Cel: +52 (33) 1042 8382 rsalazar@innox.com.mx http://www.innox.com.mx


« previous php.general.es (#56298) next »