Page 1 of 1

On-Click Downloading

Posted: Fri Feb 03, 2006 2:43 pm
by LiveFree
Hello!

I have a PHP script that gets all the Downloads from one catagory. But I need to have the filename for each file clicable and when it is clicked, that file downloads.

Thanks :)
I dont really think this can be done w/ PHP

Posted: Fri Feb 03, 2006 2:49 pm
by Burrito
what kind of file are you trying to download? Most files (that can't be opened in a browser) just require a link to them.

take a look at header() too...

Posted: Fri Feb 03, 2006 2:54 pm
by LiveFree
They are mostly movie files...

Wouldnt I need JS? Or is there some click functions that executes a specific function (in my case, to force a DL) in PHP I dont know of

Posted: Fri Feb 03, 2006 2:57 pm
by josh

Code: Select all

// force to download a file
$file = "http://localhost/test/".$_GET['file']."";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);

Posted: Fri Feb 03, 2006 2:59 pm
by LiveFree
Yea,

Ill put that into a function, but I need a little snippet where (remember this is in a while loop from every DB row) when a user clicks on a link (NOT A BUTTON), the file is downloaded.

Like I said, I think its JS

Posted: Fri Feb 03, 2006 3:00 pm
by Burrito
no the code jshpro2 posted would be your target page, you'll need to fetch file info based on what link is clicked then send the file down.

if they are movie files you should just be able to link to the file itself though 8O

Posted: Fri Feb 03, 2006 3:02 pm
by LiveFree
Oh Damnit

Thanks Guys

/me Kicks myself for not seeing that

My experiance has betryared me :(

Posted: Fri Feb 03, 2006 3:05 pm
by josh
I must mention if you are going to use the code I posted you need to make sure $file is not your database connection script or something confidential

Posted: Sat Feb 04, 2006 5:58 pm
by LiveFree
Okay,

I have made it work ... so so

Code: Select all

<?php
session_start();

// force to download a file
$source=$_SESSION['file'];
$file = "videos/$source";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=$file");

header( "Content-Description: File Transfer");
@readfile($file); 

?>
When you click on the download link, the file name is always like so: video-filename.jpg
NOTE** It does actually put in the right filename, just the video- part is stuck on

Thanks for teh code though :)