I'm trying to download a file from the server in PHP, I'm working with Wordpress, this is my code
<?php
// Genera la descarga del convenio colectivo de trabajo
$dirBase = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/accelerate/convenioColectivo/convenio_colectivo";
// Veo si es un .doc, un .docx o un .pdf
if (file_exists($dirBase . ".doc")) {
$ext = ".doc";
$ctype = "application/msword";
} else {
if (file_exists($dirBase . ".docx")) {
$ext = ".docx";
$ctype = "application/msword";
} else {
if (file_exists($dirBase . ".pdf")) {
$ext = ".pdf";
$ctype = "application/pdf";
} else
exit();
}
}
$archivo = $dirBase . $ext;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: " . $ctype); // Pongo el tipo de informacion correspondiente
header("Content-Disposition: attachment; filename=convenio_colectivo" . $ext ); // Que se descargue con el nombre convenio_colectivo + extension correspondiente siempre
header("Content-Transfer-Encoding: binary");
header("Content-Length: ". filesize($archivo)); // Traiga solo lo que pesa el archivo
ob_clean();
flush();
readfile( $archivo );
?>
In the Localhost it works perfectly and downloads without problems . But when I host it, this happens:
I am being printed on the screen instead of downloading Does anyone have any idea how to solve this problem?