PHP Application - Problems downloading files in xlsx

0

Hello, I have an application that is having problems downloading files in the latest Excel format

Displays the

Intheapplicationcallthisfileinphp

IntheuploadIhavenoproblemssavingthefilesinsidetheapplication,inafoldercalledfiles..andwhenIaccessthefolderviawindowssameandopenthefiledoesnotgivestick,nowwhenIclickondowloadthatcallsthisclass  Whatcanbeguys??

download.phpseethecode

<?phprequire("config.inc.php");

 try {

   // Inicializando a Fachada
$objFachadaBDR = FachadaBDR::getInstance();

// Recuperando os dados postados
$strHashIdHistorico = (string) utf8_decode(@$_GET["strHashIdHistorico"]);

 // Validando o identificador do historico
$intIdHistorico = Encripta::decode($strHashIdHistorico, "downloadAnexo");

 if (!$intIdHistorico) throw new Exception("O Identificador do  
  Hist&oacute;rico est&aacute; incorreto. Tente o acesso novamente.");

   / Recuperando o historico
$objHistorico = $objFachadaBDR->procurarHistorico($intIdHistorico);

 // Montando o caminho do arquivo anexo
$strCaminhoArquivoAnexo = Config::getCaminhoArquivosAnexos() ."/". 
 $objHistorico->getCaminhoArquivoAnexo();

 if (strlen(trim($objHistorico->getCaminhoArquivoAnexo())) == 0 || strlen(trim($objHistorico->getNomeArquivoAnexo())) == 0) throw new Exception("O arquivo solicitado n&atilde;o foi encontrado.");



if (!file_exists($strCaminhoArquivoAnexo)) throw new Exception("O arquivo solicitado (". $objHistorico->getNomeArquivoAnexo(). ") n&atilde;o existe.");



 // Tamanho do arquivo e extens�o
$intTamanhoArquivo = filesize($strCaminhoArquivoAnexo);


$arrArquivoAnexo = pathinfo($strCaminhoArquivoAnexo);
$strExtensao = ""; 

if (!empty($arrArquivoAnexo["extension"])) $strExtensao = 
      strtolower($arrArquivoAnexo["extension"]);




 // Determinando o Content-Type do arquivo
$strContentType = "";
switch ($strExtensao) {
    case "pdf": $strContentType = "application/pdf"; break;
    case "exe": $strContentType = "application/octet-stream"; break;
    case "zip": $strContentType = "application/zip"; break;
    case "doc": $strContentType = "application/msword"; break;
    case "xls": $strContentType = "application/vnd.ms-excel"; break;//application/vnd.ms-excel
    case "xlsx":$strContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;//ricardo
    case "ppt": $strContentType = "application/vnd.ms-powerpoint"; break;
    case "gif": $strContentType = "image/gif"; break;
    case "png": $strContentType = "image/png"; break;
    case "jpeg":
    case "jpg": $strContentType = "image/jpg"; break;

    default: $strContentType = "application/force-download";
}

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: ". $strContentType);
header('Content-Disposition: attachment; filename="'. $objHistorico->getNomeArquivoAnexo() .'";');
header("Content-Transfer-Encoding: binary");
//header("Content-Type: application/vnd.ms-excel; charset=utf-8");

header("Content-Length: ". $intTamanhoArquivo);
readfile($strCaminhoArquivoAnexo); 




}catch (Exception $ex) {
$strMensagem = htmlentities($ex->getMessage());
include("includes/incTopo.php");
include("includes/incErro.php");
include("includes/incRodape.php");
exit();

}

    
asked by anonymous 09.07.2018 / 16:03

1 answer

0

This link was able to get the answer I needed ... I updated my headers ..

link Response

header('Content-Description: File Transfer');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".basename($strCaminhoArquivoAnexo)."\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Length: ' . filesize($strCaminhoArquivoAnexo)); //Remove

ob_clean();
flush();

readfile($strCaminhoArquivoAnexo);i
    
09.07.2018 / 17:31