Jump to content

Recommended Posts

I have a phpfile that is reading different kinds of files that I want to force download. My switch case statements are working and it is downloading them, however, its spitting out all this binary code. Any idea why that is so? I am just trying to force the download. thanks.

<?php
session_start();
if ($_SESSION['approved'] != 'yes') {
header("Location: dlerror.php");
} else {
$filename = $_POST['fname'];
$filename = realpath("8d46y2g1/".$filename);
echo($filename);

$file_extension = strtolower(substr(strrchr($filename,"."),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;
	default: $ctype="application/force-download";
}

if (!file_exists($filename)) {
	die("NO FILE HERE");
}
echo($ctype);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
set_time_limit(0);
@readfile("$filename") or die("File not found.");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/71274-solved-headers-and-readfile/
Share on other sites

When I made some force-download scripts, I assisted with the script in this tutorial. Make sure you have everything that it has there. I think if you will change your script this way, it will work:

 

<?php
session_start();
if ($_SESSION['approved'] != 'yes') {
header("Location: dlerror.php");
} else {
$filename = $_POST['fname'];
$filename = realpath("8d46y2g1/".$filename);

$file_extension = strtolower(substr(strrchr($filename,"."),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;
	default: $ctype="application/force-download";
}

if (!file_exists($filename)) {
	die("NO FILE HERE");
}

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
	ini_set('zlib.output_compression', 'Off');
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-Transfer-Encoding: binary");
header("Content-Length: ".filesize(basename($filename)));
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
set_time_limit(0);
@readfile("$filename") or die("File not found.");
}
?>

 

 

Orio.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.