Jump to content

[SOLVED] File downloader


robcrozier

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");

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.