Jump to content

force download script failing with PDFs.


spiderwell

Recommended Posts

Hi guys

 

This one has been trouble for me for a qhile now, and I have tried various methods and none seem to work on pdf downloads, the file downloads only 1 byte or there abouts. All other files download ok , just PDFs seem not to.

here is my code, the $filecontrol->insertFileDownload() are  just database logs being inserted

 

 

<?php
ob_start();
session_start();
include("../classes/files.php");
$filecontrol = new FileControl();
$var_fileid = $_GET['fileid'];
$userid = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 0;
if ($userid == 0) die('you are not logged in');
if ($filecontrol->isFilePublic($var_fileid))
{
$file = "../filedump/" . $filecontrol->getFileName($var_fileid);
$filecontrol->insertFileDownload($var_fileid,$userid,true);

}
else
{
if ($filecontrol->doesUserHaveRights($var_fileid))
{
	$file = "../filedump/" . $filecontrol->getFileName($var_fileid);
	$filecontrol->insertFileDownload($var_fileid,$userid,true);

}
else
{
	$file = "../filedump/" . "norights.txt";
	$filecontrol->insertFileDownload($var_fileid,$userid,false);
}
}
$file_extension = strtolower(substr(strrchr($file,"."),1));
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}

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");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile("$file");
exit();

 

thanks for reading

Link to comment
https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.