Forum Moderators: phranque

Message Too Old, No Replies

Htaccess for download directory

How can i restrict the file downloads within the .htaccess?

         

asantos

8:58 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Hi.

I have a /download directory for my website. I dont want the files to get downloaded just by typing [foo.com...] because its sensitive information.

I was wondering if there's a way to DISABLE the file download through the .htaccess, and then i could get the file with


http://www.foo.com/get.php?id=$ID_OF_FILE&hash=MD5($filename)

Thanks,
Andres S.

coopster

9:59 pm on Jun 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your best bet would be to keep the documents outside of the DocumentRoot but if you must keep them there then you can password protect the directory and/or protect it with an Order [httpd.apache.org] Deny/Allow directive.
Order Deny,Allow  
Deny from all

Mokita

3:34 am on Jun 9, 2006 (gmt 0)

10+ Year Member



Your best bet would be to keep the documents outside of the DocumentRoot

coopster: Would you explain, or better still post the code, for how to call a file from a directory outside of root please?

I've been wanting to implement this for some time, but haven't been able to find out how.

TIA.

asantos

3:55 am on Jun 9, 2006 (gmt 0)

10+ Year Member



Mokita:

Call:
<img src="img.php?tipo=album&img=hola.jpg" />

DIR_UPLOAD is a directory outside /public_html

Modify this code for your needs:******************


<?php
if(!$_GET['img'] ¦¦!$_GET['tipo']) die();

$img = new img();
if($file = realpath(DIR_UPLOAD.basename($_GET['tipo']).'/'.basename($_GET['img']))) {
# Fichero existe
$img->load($file);
$img->resize(intval($_GET['s']),$_GET['c']);
$img->show();
} else {
# Fichero no existe
$img->blank(intval($_GET['s']));
}

//Gestionador de imagenes
//***********************
class img {
var $imagen;
var $temp;
var $ext;
var $size;

//Constructor
//***********
function img() {
# Define dimension predeterminada
$this->size = 100;
}

//Loader
//******
function load($file) {
# Obtiene extension de imagen
$info = pathinfo($file);
$this->ext = $info['extension'];
switch($this->ext) {
# GIF
case 'gif':
$this->imagen = ImageCreateFromGIF($file);
break;
# JPEG
case 'jpe': case 'jpeg': case 'jpg':
$this->imagen = ImageCreateFromJPEG($file);
break;
# PNG
case 'png':
$this->imagen = ImageCreateFromPNG($file);
break;
# ...
default: $this->blank(); return;
}
}

//Impresion de imagen
//*******************
function show() {
switch($this->ext) {
# GIF
case 'gif':
header('Content-Type: image/gif');
Imagegif($this->imagen);
break;
# JPEG
case 'jpe': case 'jpeg': case 'jpg':
header('Content-Type: image/jpeg');
Imagejpeg($this->imagen);
break;
# PNG
case 'png':
header('Content-Type: image/png');
Imagepng($this->imagen);
break;
# ...
default: $this->blank();
}
}

# ***************************************************************************************** #

//Resize
//******
function resize($size=false,$cuadrado=false) {
$size = (!is_numeric($size) ¦¦ $size<1)? $this->size : $size;
# Valores originales
$width_original = imagesx($this->imagen);
$height_original = imagesy($this->imagen);
if($cuadrado) {
# Cuadrado
$width = $height = $size;
} else {
# Relativo
$width = $size;
$height = round($height_original * $width / $width_original);
}
# Crea plantilla de nueva imagen
$this->temp = imageCreateTrueColor($width,$height);
# Crea copia con nuevo tamano
imageCopyResampled($this->temp,$this->imagen,0,0,0,0,$width,$height,$width_original,$height_original);
# Asignar valor de nueva imagen (por cursor)
$this->imagen =& $this->temp;
unset($this->temp);
return;
}

//Impresion de imagen default
//***************************
function blank($size=false) {
$size = (!is_numeric($size) ¦¦ $size<1)? $this->size : $size;
header('Content-Type: image/png');
$blank = imagecreatetruecolor($size,$size) or die();
$color = imagecolorallocate($blank,255,255,255);
imagestring($blank,5,4,4,'?',$color);
imagepng($blank);
imagedestroy($blank);
}

# ***************************************************************************************** #

//Marca de Agua
//*************
function watermark($file,$left=0,$top=0) {
ImageAlphaBlending($this->imagen, true);
$layer = ImageCreateFromPNG($file);
$w = ImageSX($layer);
$h = ImageSY($layer);
ImageCopy($this->imagen,$layer,$left,$top,0,0,$w,$h);
}

//Texto
//*****
function texto($texto,$size=12,$font='arial.ttf') {
$white = imagecolorallocate($this->imagen, 255, 255, 255);
imagettftext($this->imagen, $size, 0, 10, 10, $white, $font, $texto);
}
}
?>