RuiGomes Posted April 9, 2012 at 01:49 AM Report #448162 Posted April 9, 2012 at 01:49 AM Boa noite, Estou a fazer um sistema de upload/download. Por uma questão de segurança, guardo os ficheiros fora de uma pasta acessível via web e é o PHP que serve o download ao cliente. Basicamente tenho um ficheiro que aponta para downloader.php?file=123.zip O código do downloader.php é: $path = getUploadPath(); $filename = getFileName($fileid); $dl_file = $path.$filename; $dl_file_size = filesize( $dl_file ); $dl_file_offset = 0; $dl_resumed = isset( $_SERVER['HTTP_RANGE'] ) ? true : false; header( 'Cache-Control: public' ); header( 'Content-Transfer-Encoding: binary' ); header( 'Content-Type: application/octet-stream' ); header( 'Content-Disposition: attachment; filename="' . basename( $dl_file ) . '"' ); header( 'Accept-Ranges: bytes' ); if($dl_resumed) { preg_match( '/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $preg_match ); $dl_file_offset = intval( $preg_match[1] ); $dl_file_end = intval( $preg_match[2] ); header( 'HTTP/1.1 206 Partial Content' ); header( 'Content-Length: ' . $dl_file_end-$dl_file_offset+1 ); header( 'Content-Range: bytes ' . $dl_file_offset . '-' . $dl_file_end . '/' . $dl_file_size ); } else { header( 'Content-Range: bytes 0-' . $dl_file_size-1 . '/' . $dl_file_size ); header( 'Content-Length: ' . $dl_file_size ); } ignore_user_abort( true ); $dl_file_handle = fopen( $dl_file, 'rb' ); fseek( $dl_file_handle, $dl_file_offset ); while( !feof( $dl_file_handle ) && ( connection_aborted() == 0 ) ) { set_time_limit(0); echo fread( $dl_file_handle, 1024*$dl_speed ); sleep(1); ob_end_flush(); ob_flush(); flush(); ob_start(); } fclose( $dl_file_handle ); die; E isto dá sempre 500 Internal Server Error. Poderá ser um problema do alojamento (PTWS)? Ou é o código que está de alguma forma mal? Há alguma forma mais fácil de fazer isto?
taviroquai Posted April 9, 2012 at 03:41 AM Report #448169 Posted April 9, 2012 at 03:41 AM Viva, Para depurar, no inicio do ficheiro coloca instruções para mostrar os erros: error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', 1); E comenta o header para o tipo de conteudo... //header( 'Content-Type: application/octet-stream' );
RuiGomes Posted April 9, 2012 at 03:47 AM Author Report #448170 Posted April 9, 2012 at 03:47 AM Viva, Para depurar, no inicio do ficheiro coloca instruções para mostrar os erros: error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', 1); E comenta o header para o tipo de conteudo... //header( 'Content-Type: application/octet-stream' ); Continua a dar 500 Internal Server Error, mas já consegui ver o que está mal no error_log: [09-Apr-2012 04:45:04] PHP Notice: ob_flush() [<a href='ref.outcontrol'>ref.outcontrol</a>]: failed to flush buffer. No buffer to flush. in /home/xxxx/public_html/xxx.com/downloader.php on line 53 Ou seja, o problema está neste ciclo: while( !feof( $dl_file_handle ) && ( connection_aborted() == 0 ) ) { set_time_limit(0); echo fread( $dl_file_handle, 1024*$dl_speed ); sleep(1); ob_end_flush(); ob_flush(); flush(); ob_start(); } Nomeadamente no ob_flush(), mas não faço ideia o que será. Alguém me dá umas luzes?
mjamado Posted April 9, 2012 at 09:22 AM Report #448177 Posted April 9, 2012 at 09:22 AM Nomeadamente no ob_flush(), mas não faço ideia o que será. Alguém me dá umas luzes? Está lá na mensagem de erro, clarinho como água: No buffer to flush Acho que a partir daqui já te consegues safar. "Para desenhar um website, não tenho que saber distinguir server-side de client-side" - um membro do fórum que se auto-intitula webdesigner. Temo pelo futuro da web.
RuiGomes Posted April 9, 2012 at 12:56 PM Author Report #448195 Posted April 9, 2012 at 12:56 PM Está lá na mensagem de erro, clarinho como água: Acho que a partir daqui já te consegues safar. Será pelo ob_start(); estar no fim do while? Tentei colocar um ob_start(); antes do ciclo mas continua igual :\
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now