How to make a unique download link

Got an idea for a tutorial ? Share it here.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

How to make a unique download link

Post by FrederickGeek8 »

This would be a tutorial on how to make a unique download link (download link that only works once then changes url)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: How to make a unique download link

Post by Temor »

This would be really easy actually.
I can write a short text tutorial on this.

Tutorial:

I will assume that you already know how to create a regular download link. If not, then watch one of Jacek's tutorials on uploading and downloading files.
I will base most of this tutorial on Jacek's Temporary Download Link tutorial ( http://betterphp.co.uk/playlist.html?pi ... 49860BA239 ).

You would have to create a database for this with a table containing these columns:
file_id - Primary key, Auto Increment // The file ID
file_code - Varchar 12 // The unique code, can be any lenght you want.
file_name - Varchar 255 // The name of the file.

Upload part:
This is pretty much identical to the code from the tutorial, except we don't have the expiry time and we need to create a unique code. This will be done using a technique Jacek uses in another tutorial ( can't remember which one ).

[syntax=php]<?php

if(isset($_FILES['file'])){

$file_name = mysql_real_escape_string($_FILES['file']['name']);

// To create the code we will create a charset from an array of letters and numbers and jumble it.
$charset = array_flip(array_merge(range('a','z'), range('A','Z'), range(0, 9)));
// The unique code is then created from taking 12 letters and numbers from the charset we just made and turn them into one string.. You can change this number to anything, but remember to change the length in the table as well.
$unique_code = implode('', array_rand($charset,12));

// Insert the filename and unique code into the files table.
mysql_query("INSERT INTO `files` (`file_code`, `file_name`) VALUES('{$unique_code}', '{$file_name}') ");

move_uploaded_file($_FILES['file']['tmp_name'], "core/files/{$_FILES['file']['name']}");


}

?>[/syntax]

Download part:
This is also pretty much identical to the code from the tutorial, except we will not search for a file using it's ID, but rather it's unique code. And we then have to change that code ( or delete the file ).

[syntax=php]<?php
if (isset($_GET['code'])){
$code = (int)$_GET['code'];

$file = mysql_query("SELECT `file_name` FROM `files` WHERE `file_code` = '{$code}'");

if (mysql_num_rows($file) != 1){
echo 'Invalid code';
}else{
$row = mysql_fetch_assoc($file);

$path = "core/files/{$row['file_name']}";

header('Content-Type: application/octetstream');
header('Content-Type: application/octet-stream');
header('Content-Description : File Transfer');
header("Content-Disposition: attachment; filename=\"{$row['file_name']}\"");
header('Content-Length: ' .filesize($path));

readfile($path);

// Create a new random code.
$charset = array_flip(array_merge(range('a','z'), range('A','Z'), range(0, 9)));
$new_code = implode('', array_rand($charset,12));

// Update the files table and change the file_code to our new code where file_code is the same as the code from the URL.
// You could run a delete query here instead.
mysql_query("UPDATE `files` SET `file_code` = '{$new_code}' WHERE `file_code` = '{$code}' ");

}
}
?>[/syntax]

If you require further explanation, please say so :)
Post Reply