robcrozier Posted February 3, 2009 Share Posted February 3, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/143590-solved-file-downloader/ Share on other sites More sharing options...
MadTechie Posted February 3, 2009 Share Posted February 3, 2009 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"); Quote Link to comment https://forums.phpfreaks.com/topic/143590-solved-file-downloader/#findComment-753410 Share on other sites More sharing options...
robcrozier Posted February 4, 2009 Author Share Posted February 4, 2009 Awesome mate! Worked a treat! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/143590-solved-file-downloader/#findComment-754176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.