php - empty file in force download - what is wrong? -


i using following code force download files in php

<?php     $id =$_get['id'];  $query = mysql_query("select * `blog`  id = '" . $id . "' limit 1")or die(mysql_error()); while($row = mysql_fetch_assoc($query)){          $file_name = $row[url]; //  "wwyypyv6.pdf"         $file_type = $row[type]; // "application/pdf"         $file_url = 'http://emfhal.hostech.co.il/upload/' . $file_name;     }         header("content-type: " . $file_type);         header("content-disposition: attachment; filename=\"$file_name");         readfile($file_url);         exit();       ?> 

when use code download pdf file (only used testing purposes), open downloaded , , gives me file empty. tried in chrome. opening windows , google chrome, says can't display pdf file because empty???

correct solution:

// grab requested file's name $file_name = $_get['file'];  // make sure it's file before doing anything! if(is_file($file_name)) {      /*         processing you'd here:         1.  increment counter         2.  db         3.  check user permissions         4.  want!     */      // required ie     if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'off'); }      // file mime type using file extension     switch(strtolower(substr(strrchr($file_name, '.'), 1))) {         case 'pdf': $mime = 'application/pdf'; break;         case 'zip': $mime = 'application/zip'; break;         case 'jpeg':         case 'jpg': $mime = 'image/jpg'; break;         default: $mime = 'application/force-download';     }     header('pragma: public');   // required     header('expires: 0');       // no cache     header('cache-control: must-revalidate, post-check=0, pre-check=0');     header('last-modified: '.gmdate ('d, d m y h:i:s', filemtime ($file_name)).' gmt');     header('cache-control: private',false);     header('content-type: '.$mime);     header('content-disposition: attachment; filename="'.basename($file_name).'"');     header('content-transfer-encoding: binary');     header('content-length: '.filesize($file_name));    // provide file size     header('connection: close');     readfile($file_name);       // push out     exit(); 

}


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -