multiplos arquivos para download no PHP
24/09/2017
0
Pessoal boa noite, estou tentando fazer um script para download de 2 arquivos que estão em uma pasta local. Um PDF e outro XML.
Consegui com bastante pesquisa aqui no forum fazer a busca e enviar o arquivo para download, porem ele só faz o download do primeiro arquivo.
Infelizmente não sei nada de PHP, iria desenvolver em java que tenho conhecimento, mas não tenho servidor de aplicação web java pronto.
Mas tenho de PHP por isso estou tentando.
Desculpem a gambiarra srsrsr
código a baixo.
Arquivo index.php
Arquivo baixar2.php
Consegui com bastante pesquisa aqui no forum fazer a busca e enviar o arquivo para download, porem ele só faz o download do primeiro arquivo.
Infelizmente não sei nada de PHP, iria desenvolver em java que tenho conhecimento, mas não tenho servidor de aplicação web java pronto.
Mas tenho de PHP por isso estou tentando.
Desculpem a gambiarra srsrsr
código a baixo.
Arquivo index.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="baixar2.php" method="GET"> buscar arquivos com.. <input type="text" name="arquivo" value=""> <input type="submit" name="busca"> </form> </body> </html>
Arquivo baixar2.php
<?php //$pasta = 'C:\\xampp\\htdocs\\nfe\\arquivos\\\\'; function download($pos,$pasta){ $xmlNfe = $pasta."Nfe".$_GET["arquivo"].$pos; $testa = substr($xmlNfe,-3); $bloqueados = array('php','tml','htm'); // caso a extensão seja diferente das citadas acima ele // executa normalmente o script if(!in_array($testa,$bloqueados)){ if(isset($xmlNfe) && file_exists($xmlNfe)){ // faz o teste se a variavel não esta vazia e se o arquivo realmente existe switch(strtolower(substr(strrchr(basename($xmlNfe),"."),1))){ // verifica a extensão do arquivo para pegar o tipo case "pdf": $tipo="application/pdf"; break; case "exe": $tipo="application/octet-stream"; break; case "zip": $tipo="application/zip"; break; case "doc": $tipo="application/msword"; break; case "xls": $tipo="application/vnd.ms-excel"; break; case "ppt": $tipo="application/vnd.ms-powerpoint"; break; case "gif": $tipo="image/gif"; break; case "png": $tipo="image/png"; break; case "jpg": $tipo="image/jpg"; break; case "mp3": $tipo="audio/mpeg"; break; case "xml": $tipo="text/xml"; break; case "php": // deixar vazio por seurança case "htm": // deixar vazio por seurança case "html": // deixar vazio por seurança } header("Content-Type: ".$tipo); // informa o tipo do arquivo ao navegador header("Content-Length: ".filesize($xmlNfe)); // informa o tamanho do arquivo ao navegador header("Content-Disposition: attachment; filename=".basename($xmlNfe)); // informa ao navegador que é tipo anexo e faz abrir a janela de download, tambem informa o nome do arquivo readfile($xmlNfe); // lê o arquivo //exit; // aborta pós-ações } }else{echo "Chave da NFE não localizada, ou digitada incorretamente!";exit;} } download("-nfe.xml",'C:\\xampp\\htdocs\\nfe\\arquivos\\\\'); download("-nfe.pdf",'C:\\xampp\\htdocs\\nfe\\arquivos\\\\'); exit; ?>
Leonardo Freitas
Curtir tópico
+ 0
Responder
Posts
24/09/2017
Leonardo Freitas
Fiz a seguinte modificação.
index.php
baixar.php
index.php
<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="baixar.php" method="GET"> buscar arquivos com.. <input type="text" name="arquivo" value=""> <input type="submit" name="busca"> </form> </body> </html>
baixar.php
<?php $pasta = 'C:\\xampp\\htdocs\\nfe\\arquivos\\\\'; $xmlNfe = $pasta."Nfe".$_GET["arquivo"]."-nfe.xml"; $pdfNfe = $pasta."Nfe".$_GET["arquivo"]."-nfe.pdf"; if(isset($xmlNfe) && file_exists($xmlNfe) && ($pdfNfe) && file_exists($pdfNfe) ){ // faz o teste se a variavel não esta vazia e se o arquivo realmente existe $zip = new ZipArchive(); if( $zip->open( 'C:\\xampp\\htdocs\\nfe\\zips\\arquivo.zip',ZipArchive::CREATE ) === true){ $zip->addFile($pdfNfe , 'Danfe.pdf'); $zip->addFile($xmlNfe , 'xml.xml'); $zip->close(); } $dir = 'C:\\xampp\\htdocs\\nfe\\zips\\\\'; $arquivo = 'arquivo.zip'; $var = "application/force-download"; if(file_exists($dir.$arquivo)){ header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: $var"); header("Content-Disposition: attachment; filename=\\"".basename($dir.$arquivo)."\\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($dir.$arquivo)); readfile($dir.$arquivo); } unlink($dir.$arquivo); }else{echo "Chave da NFE não localizada, ou digitada incorretamente!";exit;} exit; ?>
Responder
Clique aqui para fazer login e interagir na Comunidade :)