i was looking for a similar solution. After searching google.. i wrote this simple code. This code might give you some hint. It's not the good coding. Please note you have to put your files outside root directory or public site.
I want to know if there are any file size restriction on readfile function.
Will it crash shared hosting server? I will have more then 300MB of mpeg file.
I want to embed mpeg file in html file and video source will be like protect.php?file=myvideo.mpg
If anyone can give us more information would be great.
<?php
require_once("mainfile.php");
$path="D:\webserver\Apache\\";
$protect = new Protect($_GET["file"],$path);
if(is_authorised()){
$protect->downloadfile();
}else{
die("Please login");
}
class Protect{
var $file;
var $path;
function Protect($file,$path){
$this->file=$file;
$this->path=$path;
}
function checkFile(){
if(!empty($this->file) && !eregi("http://",$this->file) && $this->getmimetype($this->file)){
if(file_exists($this->path.$this->file) && is_readable($this->path.$this->file)){
return true;
}else{
return false;
}
}else{
return false;
}
}
function showfile(){
if($this->checkFile()){
header('Content-type: '.$this->getmimetype());
header('Content-transfer-encoding: binary');
header('Content-length: '.filesize($this->path.$this->file));
readfile($this->path.$this->file);
}
}
function downloadfile(){
if($this->checkFile()){
header('Content-Description: File Transfer');
header('Content-type: '.$this->getmimetype());
header('Content-transfer-encoding: binary');
header('Content-length: '.filesize($this->path.$this->file));
header('Content-Disposition: attachment; filename=' . basename($this->path.$this->file));
readfile($this->path.$this->file);
}
}
function getmimetype(){
$file=explode(".",$this->file);
switch($file[1]){
case"jpe":
case"jpg":
return "image/jpeg";
break;
case"mpg":
return "video/mpeg mpeg mpg mpe";
break;
case"ppt":
return "application/vnd.ms-powerpoint ppt";
break;
case"zip":
return "application/zip zip";
break;
case"gif":
return "image/gif gif";
break;
case".doc":
return "application/msword doc";
break;
case"xls":
return "application/vnd.ms-excel";
break;
case"pdf":
return "application/pdf";
break;
case"png":
return "image/png";
break;
case"exe":
return "application/octet-stream";
break;
}
}
}
?>