need help with creating download links

Ask about a PHP problem here.
Post Reply
toufail
Posts: 5
Joined: Sun Mar 04, 2012 6:13 pm

need help with creating download links

Post by toufail »

hi I'm new to PHP i am trying to create a link for users so they can download their own file from the server which they have previously uploaded, similarly the way user can delete their own files i need some help, what do i have to do i created a page called download.php and i need help with download.func.php

here are my files

download.php

<?php
include 'init.php';

if (!logged_in())
	{
		header('Location: index.php');
		exit();	
	}
	
if (image_check ($_GET['image_id']) === false)
	{
		header('Location:view_album.php?album_id=', $album['id']
		exit();
	}

if (isset ($_GET['image_id']) || empty($_GET['image_id']))
	{
		$image_id = $_GET['image_id'];
		download_image($_GET['$image_id']);
		header('Location: '.$_SERVER['HTTP_REFERER']);
		exit();
		
	}	

?>

download.func.php
 function download_image($image_id) 
	{
		$image_id = (int)$image_id;
		$image_query=  mysql_query (" SELECT `album_id`, `ext`, FROM `images`  WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id'] );
		
		$image_result = mysql_fetch_assoc($image_query);
		$album_id =  $image_result['album_id'];
		$image_ext =  $image_result['ext'];
		// need help here dont know how to create link to files
		
	}  	


?>


then i put the code in my view_album.php
</a> [<a href="download_image.php?image_id=', $image['id'], '">Download Image</a>
can someone help me out, i would like to know if i am doing it right
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: need help with creating download links

Post by jacek »

I would probably replace the function with somethign like get_image() that would return the location of the file on the server. Once you know the path yo can send some headers which makes the browser thing it needs to download a file
header('Content-Type: application/octetstream'); // <-- this is for stupid IE6
header('Content-Type: application/octet-stream');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Length: ' . filesize($path));

// php4 method used here because large files would use a lot or RAM with readfile.
$file = fopen($path, 'rb');

while (feof($file) === false){
	echo fread($file, 4096);
}

fclose($file);
die(); // <-- leave this out if you are at the end of the script anyway.
Image
toufail
Posts: 5
Joined: Sun Mar 04, 2012 6:13 pm

Re: need help with creating download links

Post by toufail »

thanks for the reply well i did the following


download_image.php
<?php
include 'init.php';


if (!logged_in()) {
	header('Location: index.php');
	exit();
}

if (image_check($_GET['image_id']) == false) {
	echo 'stopped at check'; //--  header('Location: albums.php');
	exit();
}

if (isset($_GET['image_id']) || empty($_GET['image_id'])) {
	download_image($_GET['image_id']);
	echo 'stopped at download'; //--  header('Location: '.$_SERVER['HTTP_REFERER']);
	exit();
}

?>


function download_image
function download_image($image_id) 
	{
		$image_id = (int)$image_id;
		$image_query=  mysql_query (" SELECT `album_id`, `ext`, FROM `images`  WHERE `image_id`=$image_id AND `user_id`=".$_SESSION['user_id'] );
		
		$image_result = mysql_fetch_assoc($image_query);
		$album_id =  $image_result['album_id'];
		$image_ext =  $image_result['ext'];
		
                //$file = $image['id'];         
                //$file = fetch file data from db based off of 
                $_GET['image_id'];
                header("Content-Type: application/force-download"); 
                header('Content-Description: File Transfer'); 
                readfile($image_id);

		
	}

but now the problem l am facing is that its not downloading the image i want rather its downloading the page where the code is executed from which is download_image.php
Last edited by toufail on Thu Mar 08, 2012 6:45 pm, edited 1 time in total.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: need help with creating download links

Post by jacek »

That's how that works, it forces the download of the page. But the page contains an image because you read it with readfile (ignoring my advice :() Also it looks like you are passing the wrong file path into readfile(), is the image really stored in the same folder as the script with the name the same as it's ID from the database ?
Image
toufail
Posts: 5
Joined: Sun Mar 04, 2012 6:13 pm

Re: need help with creating download links

Post by toufail »

The files are stored in a folder called uploads, when a user creates an album it uses the users I'd, when they upload a image it goes into the album example uploads\userid\imageid the script is in the root directory of the site, would it be possible if I give u my server end details, via pm that u can log in and have a look at the files and you can get a clear understanding what I am trying to do or what's happening. I would really appreciate it if you can have a look
Thank you
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: need help with creating download links

Post by jacek »

Okay. Well readfile() expects a file path, not an image ID. you you need to work out that path to the image and pass that in instead. it will probably just be a matter of adding the folder too.
Image
toufail
Posts: 5
Joined: Sun Mar 04, 2012 6:13 pm

Re: need help with creating download links

Post by toufail »

jacek wrote:Okay. Well readfile() expects a file path, not an image ID. you you need to work out that path to the image and pass that in instead. it will probably just be a matter of adding the folder too.
my files are saved in the directory with its id not the name

i tried a different approach which is when the download link is clicked it creates a zip files and moves to downloads folder, and the file is downloaded form the directory

down_load.php
<?php
require('init.php');

if(isset($_GET['download'])){
	include_once('func/zip.php');
	$d_load = (int)$_GET['download'];
	//----------------------
	clear_folder();
	
	if($row = check_owner_ship($d_load)){
		//------------------------
		if($link = make_zip($row)){
			header('Location:'.$link);
		}
		//------------------------
	} else {
		if(!header('Location:'.$_SERVER['HTTP_REFERER'])){
			header('Location: folders.php'); // Or where ever you want
		}
	}
//----------------------
} else {
	if(!header('Location:'.$_SERVER['HTTP_REFERER'])){
			header('Location: folders.php'); // Or where ever you want
		}
}


function clear_folder(){
	$files = scandir('downloads');
	unset($files[0], $files[1]);
	foreach($files as $file){
		unlink('downloads/'.$file);
	}
}

function check_owner_ship($d_load){
	if($q = mysql_query("SELECT * FROM `files` WHERE `file_id`='{$d_load}' AND `user_id`=".$_SESSION['user_id'])){
		return $row = mysql_fetch_row($q);
	} else {
		return false;
	}
}

function make_zip($row){
	$t = time();
		copy('uploads/'.$row[2].'/'.$row[0].'.'.$row[4], 'downloads/'.$row[0].'.'.$row[4]);

		$files_to_zip = array(
			'downloads/'.$row[0].'.'.$row[4] );
	
		//if true, good; if false, zip creation failed
		if(create_zip($files_to_zip, 'downloads/'.$row[3].'.zip')){
			return $link = 'downloads/'.$row[3].'.zip';
		} else {
			return false;
		}
}
?>

zip.php

<?php

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
	//if the zip file already exists and overwrite is false, return false
	if(file_exists($destination) && !$overwrite) { return false; }
	//vars
	$valid_files = array();
	//if files were passed in...
	if(is_array($files)) {
		//cycle through each file
		foreach($files as $file) {
			//make sure the file exists
			if(file_exists($file)) {
				$valid_files[] = $file;
			}
		}
	}
	//if we have good files...
	if(count($valid_files)) {
		//create the archive
		$zip = new ZipArchive();
		if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
			return false;
		}
		//add the files
		foreach($valid_files as $file) {
			$zip->addFile($file,$file);
		}
		//debug
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
		
		//close the zip -- done!
		$zip->close();
		
		//check to make sure the file exists
		return file_exists($destination);
	}
	else
	{
		return false;
	}
}


?>

i cant seem to make it work can you please help me out
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: need help with creating download links

Post by jacek »

Oh, are you trying to have it download every file in the users album ? Or just one image ?
Image
toufail
Posts: 5
Joined: Sun Mar 04, 2012 6:13 pm

Re: need help with creating download links

Post by toufail »

jacek wrote:Oh, are you trying to have it download every file in the users album ? Or just one image ?
it was one link but i have sorted it out m8 thanks :D
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: need help with creating download links

Post by jacek »

Ah okay, good good :D
Image
Post Reply