PHP Application -File Download Problems in xlsx

Asked

Viewed 972 times

0

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

Displays the message from inserir a descrição da imagem aqui

in the application calls this file in php

in the upload I have no problems saving the files inside the application , in a folder called files .. and when I access the folder via windows itself and open the file does not go wrong, now when I click dowload that calls this class What can be guys ???

download php. see the code

<?php  
  require("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();

}

1 answer

0

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

link Answer

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

Browser other questions tagged

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