Readfile function dropping PHP session

4

I had a problem with file downlod, where the session was terminated when a lot of download occurred in a short time (less than 1 second), and it did not occur when the download was performed slower. ( This question can be followed here ) .

download.php file

<?php
ini_get('safe_mode');
include_once './conf.php'; 
include_once 'class/Conexao.class.php'; 
include_once 'class/Login.class.php'; 

ob_start();
$session_name = 'sec_session_id';

session_start();       
session_regenerate_id();  

//verifica o usuario logado
$log = new Login();

if(!$log->loginCheck() || $log->expired_session()) :
    $site = BASE;
    header("Location:$site");
endif;

if(!isset($_GET['id'])){   header("location: ../erro/5/");}
$id= trim(filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT));
$arq_id = filter_var($id, FILTER_SANITIZE_NUMBER_INT);
$sql = 'SELECT arq_nome, arq_desc FROM arquivo WHERE arq_id = ? LIMIT 1';
$conn = new Conexao();

if($stm = $conn->prepare($sql)):
    $stm->bind_param('i', $arq_id);
    $arq_id = $arq_id;
    $stm->execute();
    $stm->bind_result($arq_nome, $arq_desc);
    $stm->store_result();
    $stm->fetch();
    $dados = array();

    if($stm->num_rows() != 1):
        header("location: ../erro/5/");
    endif;
else:
    echo 'Erro ao selecionar arquivo';
endif;

// Arqui você faz as validações e/ou pega os dados do banco de dados

@set_time_limit(0);

$aquivoNome = $arq_nome; // nome do arquivo que será enviado p/ download
$arquivoLocal = '../../arquivos/XPtCu6bNvxOYBDYilkGL/'. $aquivoNome;

// Verifica se o arquivo não existe
if (!file_exists($arquivoLocal)) {
    // Exiba uma mensagem de erro caso ele não exista
    echo 'nao existe arquivo';
    exit;
}

// Definimos o novo nome do arquivo
$novoNome = $arq_desc;

// Configuramos os headers que serão enviados para o browser
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$novoNome.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($arquivoLocal));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');

// Envia o arquivo para o cliente
//readfile($arquivoLocal);
header('Connection: close');  
exit;
?>
<script type="text/javascript"> 
window.close();
</script>

php.ini

display_errors = Off
error_reporting = E_ERROR | E_WARNING
magic_quotes_gpc = On
memory_limit = 128M
output_buffering = 4096
post_max_size = 128M
safe_mode = On
safe_mode_exec_dir = /usr/local/www/apache22/batch
safe_mode_gid = On
upload_max_filesize = 128M
include_path = ".:/usr/local/share/phpmailer"

After the event the session was deleted, as shown below (sessiontime is my session):

[Thu Oct 06 10:14:39.915176 2016] [:error] [pid 3263] [client 10.0.0.2:52344] PHP Notice:  Undefined index: sessiontime in /var/www/html/intranet_academica/adm/index.php on line 106, referer: http://10.0.0.1/intranet_academica/adm/home/6/
  

NOTE: I had commented on the line that checks the user session and the above log occurred when I tried to access a page that the login verification was active.

After several tests, I came to the conclusion that this session crash occurs when I try to download a file without a previous download (something around 57MB).

My solution was to comment the line readfile and the problem did not occur any more, but the file was left blank. Does anyone know what the constraint on this PHP function linked to session destruction is?

    
asked by anonymous 06.10.2016 / 17:00

1 answer

0

Have you tried using a different strategy to read the file?

$fp = fopen("{$file}", "r");
fpassthru($fp);
fclose($fp);

Maybe it will resolve some limitation regarding the overload of characters that the readfile tries to generate.

    
24.10.2016 / 21:26