PHP readfile prints file on screen

2

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?

    
asked by Genarito 24.10.2016 в 17:52
source

2 answers

1

There I found the solution. Apparently the code that Wordpress adds for the templates made the download not work correctly. I just put it under my download code.

Before:

 <?php
    /*
    Template Name: Convenio Colectivo
    */ 
    /**
     * Theme Page Section for our theme.
     *
     * @package ThemeGrill
     * @subpackage Accelerate
     * @since Accelerate 1.0
     */
    ?>

<?php
    // Genera la descarga del convenio colectivo de trabajo.

    $dirBase = $_SERVER['DOCUMENT_ROOT'] . "/wordpress/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"); // required
    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 );
?>

After:

<?php
    // Genera la descarga del convenio colectivo de trabajo.
    // Se pone el otro codigo abajo para que en internet funcione correctamente la descarga

    $dirBase = $_SERVER['DOCUMENT_ROOT'] . "/wordpress/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"); // required
    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 );
?>

<?php
  /*
  Template Name: Convenio Colectivo
  */ 
  /**
   * Theme Page Section for our theme.
   *
   * @package ThemeGrill
   * @subpackage Accelerate
   * @since Accelerate 1.0
   */
 ?>
    
answered by 25.10.2016 / 03:38
source
1

The first of the calls to header () should be:

header("Content-type:application/pdf");
    
answered by 24.10.2016 в 18:18