Log in

View Full Version : Email download like like siteground.com



chetanmadaan
05-31-2009, 06:59 AM
when we go to this page http://www.siteground.com/order_template_download.php?tid=452&download=1 to download the template... We get an email with a download link which is only valid for 15 mins.

i want this same thing to happen. I know there is some database thing and php coding invloved... please help!

chetanmadaan
05-31-2009, 02:01 PM
help pleasE!

chetanmadaan
06-02-2009, 01:00 PM
where are all the xperts!

hyde360
06-07-2009, 01:21 AM
First, don't prompt people to help you... people will help you if they want to or not it's totally optional =).

First up, you will need a simple php file, to fetch the download data from within the MySQL DB. Then send the end-user an e-mail with a simple download link, which again will expire within let's say 24 hours from the download was requested.

To be honest, it's not always recommended to set it as 15 minutes because that's just ridiculous and not always will people speed for a download!

Anyways, let's call this file "downloadfile.php".



<?php

$fileID = $_GET['fileID']; //change to whatever!
$uemail = $_GET['email']; //change to whatever!

if (isset($_GET['fileID'])){

//Include a connection to the MySQL Database

include("../dbconnect.inc.php"); //Connect File, also change location to the connect file

//We need to perform a simple MySQL Query

$query = mysql_query("SELECT * FROM files WHERE fileID = '$fileID' LIMIT 1") or die('Unable to perform the MySQL Query: ' . mysql_error());
$resul = mysql_query($query) or die('Unable to perform the MySQL Query: ' . mysql_error());
$numrows = mysql_num_rows($resul);

//We'll check to wherever the file actually exists in the db.

if ($numrows == 0){//failed
echo "Unable to find the file requested!";
}

//We need to extract some data from the db.

$rows = mysql_fetch_array($resul);

$fileName = $rows["file_name"]; //The name of the file
$fileLocation = $rows["file_location"]; //The location of the file, e.g. www.mysite.com/files/template_black.zip
$fileID = $rows["fileID"]; //The file ID
$hash = "abcdefghijklmnopqrstwxy123456789ABCDEFGHIJKLMNOPQRSTWXY";
$mhash = md5($hash);

//Now we got our data, we'll insert into another DB

$insert = mysql_query("INSERT INTO fileReady (filename, filelocation, mhash, fileID) VALUES('$fileName','$fileLocation','$mhash','$fileID')") or die('error: ' . mysql_error());

if($insert){//success

//let's send the user the e-mail

$fileLink = "http://www.yoursite.com/templates/downloadfile.php?fileID=$fileID&mid=$mhash&getfile=1";

$to = "$uemail";
$subject = "Requested template from www.yoursite.com!";
$message = "
Template: $fileName is ready to be downloaded (download link expire within 24 hours)\n\n
$fileLink\n\n
Thank you for downloading from us!!!
";
$headers = "From: you <you@yoursite.com>\nReply-To: you <you@yoursite.com>";

//Now send the e-mail

mail($to, $subject, $message, $headers);

//Now we'll set a cookie to expire in 24 hours with the file mhash and filename.

$expire = time()+60*60*24; //Set to expire in 24 hours!
setcookie($fileName, $fileLink, $expire);

}

if(isset($_GET['getfile'])){

//Now for for the download

$fileQuery = mysql_query("SELECT * FROM fileReady WHERE fileID = '$fileID'") or die('error: ' . mysql_error());
$resulQy = mysql_query($fileQuery) or die('error: ' . mysql_error());
$numrows = mysql_num_rows($resulQy);

if($numrows == 0){//not found
echo "Not Found?";
}

//extract the data

$get = mysql_fetch_array($resulQy);

$fileLocation = $get["filelocation"];

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

}
}
?>


This is only a snippet i coded, and isn't secure whatsoever therefore it should really be learned by and not used as it could cause server security issues or worse MySQL injections, i've provided this as a snippet to help you so there :)

BTW: It's been tested and works, but again follow what I said above it's UNSECURE! Unless your willing to learn about security on PHP, once you got a basic knowledge of security side, put some security into this code and you'll be ready to go!

Hope this helps :cool:

chetanmadaan
06-09-2009, 06:00 PM
it never helped!

ggalan
06-09-2009, 09:56 PM
totally helped!! thank you