Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am hosting a website in which i want to make an interface in which user can upload and download files. I have done upload, but i dont know how to make files downloadable in PHP. Please help me out with this.
Posted

1 solution

below code is copied from my own software for you :)

PHP
session_start();
require_once('webapp/lib/globalfunctions/common.function.php');
function ShowFileName($filepath)
{
    preg_match('/[^?]*/', $filepath, $matches);
    $string = $matches[0];
    #split the string by the literal dot in the filename
    $pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
    #get the last dot position
    $lastdot = $pattern[count($pattern)-1][1];
    #now extract the filename using the basename function
    $filename = basename(substr($string, 0, $lastdot-1));
    #return the filename part
    return $filename;
}
$res=Varification();
if($res==false)
{
    echo "Authentication failed";
    exit;
}
if($_GET['filename']==NULL)
{
    echo "Error File name";
    exit;
}

$filename=$_GET['filename'];

$rfilename = $filename;
$base=ShowFileName($filename);

$file_extension = substr(strrchr($rfilename,"."),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 "jpe":
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
}
// echo $base;
// exit;
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=".$base.".".$file_extension.";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($rfilename));
@readfile($rfilename);
unlink($rfilename);


some of the line might have no effect, judge yourself and get proper code from this
 
Share this answer
 
Comments
[no name] 21-Jun-12 4:54am    
Thank You , I will try it out and let you know
Mohibur Rashid 21-Jun-12 4:57am    
remeber not even a single byte will be printed. even if you print a single byte the file will be screwed, and also make sure the first character in the php file must have to be .
[no name] 21-Jun-12 4:59am    
pls elobrate a bit more
Mohibur Rashid 21-Jun-12 5:01am    
example code:
--file-start-next-line
<?php
//
//all php code
//

?>--file-end-here-- (if you dont use ?> it will work too )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900