I can not download a pdf file on top of the php root

0

Hi everyone, I'm trying to download a pdf using php but I get the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home/u232864974/public_html/partials/headers.php:115) in /home/u232864974/public_html/verdetalle.php on line 141

Warning: Cannot modify header information - headers already sent by (output started at /home/4544545/public_html/partials/headers.php:115) in /home/4544545/public_html/verdetalle.php on line 144
%PDF-1.4 %äüöß 2 0 obj <> stream x��[ˎ�6��Wx=@U,Y~���m �]��Yf5��*   n6��#�IQ���t���V�m=�8<$��j���|oZ�o0�g�������������t�ul��\��u�uh���w|�?��ia��]�__~��W�,�7��P��׫i�I���^^?^�'S3��:7?7?��1����?n�����Z{7�ow���ֵ���

etcetera

And this is the code I use:

<table class="table-bordered table-striped">       
    <tr>
    <th colspan="4">Tus subidas...<label><a href="index.php">upload new files...</a></label></th>
    </tr>
    <tr>
    <td>File Name</td>
    <td>File Type</td>
    <td>File Size(KB)</td>
    <td>View</td>
    </tr>
    <?php
    $sql="SELECT * FROM uploads";
              $query = $db->prepare($sql);
 //$result->bindparam(':orden_de_compra', $orden_de_compra);

$query->execute();

    while($row = $query->fetch(PDO::FETCH_ASSOC))
    {
        ?>
        <tr>
        <td><?php echo $row['file'] ?></td>
        <td><?php echo $row['type'] ?></td>
        <td><?php echo $row['size'] ?></td>
        <td><a href="<?php echo $dir; ?>facturas/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
            <td>
                <?
            $content = $dir . 'facturas/' . $row['file'];

  header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile($content);
                ?>
            </td>        

        </tr>
        <?php
    }
    ?>

    </table>
    
asked by Daniel Treviño 08.11.2017 в 17:48
source

3 answers

1

When you upload a PDF from PHP to send to the output (screen) you have to make sure you do not send any characters (even a space) beforehand. Remove the <td><?php echo $row['file'] ?></td> and load only the PDF file.

Another solution is to save the PDF as a file and show a link to it.

I hope I have helped you

    
answered by 08.11.2017 в 18:22
1

So I solved it, create a php file without html content:

<?php
include_once 'resource/session.php';
include 'resource/Dir.php';

error_reporting (0);
          if(isset($_SESSION['username'])){
              if($_SESSION['salida_materiales'] == 1 )
{

include_once 'resource/Database.php';

if(isset($_GET['id'])) {

$id = $_GET['id'];



       $sql = "select file from uploads where id = :id "; 
$result = $db->prepare($sql); 
$result->bindparam(':id', $id, PDO::PARAM_STR);

$result->execute();
$file_name = $result->fetchColumn(); 
$ubicacion = $dir . 'facturas/' . $file_name;  



  header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='$file_name'");

// The PDF source is in original.pdf
readfile($ubicacion);




}




              }
          }

else{
    echo "No has iniciado sesion";
}
    
answered by 08.11.2017 в 19:07
-1

If I have not misunderstood, the pdf is already created and what you want is that it can be downloaded, right? The only thing you have to do is an anchor with the link of the route and the name of the pdf, that is, / invoices / nombrepdf.pdf . Check that the route is correct and of course the permissions that the directory has

    
answered by 08.11.2017 в 18:00