Jump to content

Recommended Posts

Hi guys, just wondering if anyone can help with this?

 

Basically, i have a php file downloader script set up and it works fine for most file types, however it tries to execute and executeable files as it reads their contents.  For example with php and html files.  Has anyone had this problem before and can anyone suggest how to overcome this?

 

Here's the code:

 

<?php

$root = $_SERVER['SERVER_NAME'];
$fileID = $_GET['id'];

// identify the file and file path from the database
$getFile = mysql_query("SELECT name, alias FROM files 
			WHERE files.id = '$fileID'");

while ($row = mysql_fetch_assoc($getFile))
{
$fileWithPath = $domain."".$uploadRoot."/".$row['name'];
$filePath = $domain."/".$uploadRoot;
$baseDir = $domain."/".$uploadRoot."/";
$fileName = $row['name'];
$fileAlias = $row['alias'];
$relativePath = $relativeDownloadPath.$fileName;
//$dirName = $row['dirName'];
}

if ($fd = fopen ($fileWithPath, "r")) 
{
    $fsize = filesize($relativePath);
    $path_parts = pathinfo($fileWithPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "doc":
        header("Content-type: application/msword"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "xls":
        header("Content-type: application/vnd.ms-excel"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "ppt":
        header("Content-type: application/vnd.ms-powerpoint"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "txt":
        header("Content-type: application/txt"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;

	case "gif":
        header("Content-type: image/gif"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "png":
        header("Content-type: image/png"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "jpg":
        header("Content-type: image/jpeg"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "jpeg":
        header("Content-type: image/jpeg"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;

	case "mp3":
        header("Content-type: audio/mp3"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "wav":
        header("Content-type: audio/x-wav"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;

	case "exe":
        header("Content-type: application/octet-stream"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;

	case "mpeg":
        header("Content-type: video/mpeg"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "mpg":
        header("Content-type: video/mpeg"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "mpe":
        header("Content-type: video/mpeg"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "mov":
        header("Content-type: video/quicktime"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;
	case "avi":
        header("Content-type: video/x-msvideo"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$fileAlias."\""); // use 'attachement' to force a download
        break;

        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$fileAlias."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
//exit;
?>

Link to comment
https://forums.phpfreaks.com/topic/143590-solved-file-downloader/
Share on other sites

change

        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$fileAlias."\"");

 

to

        default;
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=\"".$fileAlias."\"");
header( "Content-Description: File Transfer");

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.