The code is for loading an excel file (.xls)

Problem: After downloading, the file contains no data. then it will become empty.

So please help me with this.

<?php

    session_start();
    include_once 'oesdb.php';
    $id=$_REQUEST['id'];

    if(isset($_REQUEST['id']))
    {
      $sql=executeQuery("SELECT * FROM file where id=$id");
      $rows = mysql_fetch_array($sql);
      $file =$rows['file'];


          header('Content-Description: File Transfer');
          header('Content-Type: application/vnd.ms-excel');
          header('Content-Disposition: attachment; filename='.basename($file));
          header('Content-Transfer-Encoding: binary');
          header('Expires: 0');
          header('Cache-Control: must-revalidate');
          header('Pragma: public');
          header('Content-Length: ' . filesize($file));
          ob_clean();
          flush();
          readfile('uploads/'.$file);
          exit;

     }
?>

      

+3


source to share


3 answers


Why not create an HTACCESS file in the uploads folder and then specify

    Allow From 127.0.0.1
    Deny From All

      

Then just create a url, use the new HTML5 download feature, follow these steps:



    <a href="uploads/filenamehere.txt" download="filenamehere.txt">click to download</a>

      

This saves time trying to use PHP to load the script.

+2


source


try replacing this:

$file =$rows['file'];

      

:

$file = "uploads/".$rows['file'];

      

and this:

readfile('uploads/'.$file);

      



this

readfile($file);

      

if still doesn't work put the function return value readfile

IMPORTANT

Please pay attention to sql injection issues (see comment by Ondřej Mirtes)

0


source


The problem is here:

header('Content-Length: ' . filesize($file));

      

Content-Length

gets a null value and the browser downloads a zero length file as you told it. If $file

- path is relative upload/

, you should do this:

header('Content-Length: ' . filesize('upload/'.$file));

      

Make sure it filezise()

returns the correct size and readfile()

actually outputs it.

But another problem is that you mentioned the folder UPLOAD

and used uploads

. They are not the same, and the point is important. Also, it may 'uploads/'.$file

not be a good idea to use relative paths, it is better to use an absolute path. Eg '/var/www/upload/'.$file

.

0


source







All Articles