How to download the content blob in php

Asked

Viewed 230 times

-4

I have xml files stored in a blob field of a mysql table, I would like to know how to download these files on the local user disk automatically using php.

Create a low function on the server and not on the local disk.

function baixarArquivos($res) {
    while ($arq = mysqli_fetch_array($res)) {       
        $nomearquivo = $arq['CHAVEXML'];
        $arquivoxml  = $arq['XML'];        
        
        $path = 'c:\Temp\\xml\\'.$nomearquivo.'xml';
        file_put_contents($path, $arquivoxml);      
        
    }
}

1 answer

-1

You cannot do this on the user’s computer. What you have to do is provide the file download. Example:

<?php
    $path = 'c:\Temp\\xml\\'.$nomearquivo.'xml';
    $nomearquivo = 'seu_arquivo.xml';
    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($path));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Expires: 0');
    // Envia o arquivo para o cliente
    readfile($path);
  • Got it buddy, I need to actually download everything at once, make it really simple for the end user. A button the user presses and the system does the rest. I have this in Delphi, I needed to have the same in php or in some other language that runs with apache on linux.

  • while not dtmConexao.fdqXmlPdv.eof do&#xA;begin dtmConexao.fdqXmlPdvXML.SaveToFile('c:\teste\xml'+dtmConexao.fdqXmlPdvCHAVEXML.AsString+'.xml');&#xA; dtmConexao.fdqXmlPdv.Next;&#xA; end;

  • PHP runs on the server side. You do not have access to the client machine. Your site can create a zip file with multiple files and make available as a download. But always the user of your site will need to download.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.