Page 1 of 1

Need a little help with table for MySQL.

Posted: Thu Jul 23, 2009 10:19 am
by Alex-B
Hi to all,

Need a little help with table for MySQL.

I need to create a table that will have originale file name - that is lokated on my server in folder called "files" and need also to have a random name of the same file.

So that it looks like this one bellow

/files/excel-book.xls(real Name of the file) - excelbook
/files/macro-book.xls(real Name of the file) - macrobook
/files/codec-book.xls(real Name of the file) - codecbook

the reason I need it is to have a "download now" links on the
other pages of the site that will pint to the random name so once user will click on it than the /download.php should idealy triger the download of the file with this particular randome name (ex. <a href="http://www._____.com/download.php?urlid=excelbook">Download Now</a> than it should open /files/excel-book.xls this file)

Thanks to all for your help and thanks to this forum as well

Re: Need a little help with table for MySQL.

Posted: Thu Jul 23, 2009 5:10 pm
by andyhoneycutt
Create a table:

Code: Select all

CREATE TABLE file_name_obfuscation (
  real_filename varchar(255) NOT NULL UNIQUE PRIMARY KEY,
  fake_filename varchar(255) NOT NULL UNIQUE,
  file_type enum('excelbook','macrobook','codecbook')
);
Populate the above table with the data you want.
How to do the lookup:

Code: Select all

$dblink = mysql_connect("hostname","username","password") or die("Could not connect to the database.");
mysql_select_db("your_database",$dblink) or die("Could not select the catalog.");
$query = "SELECT fake_filename FROM file_name_obfuscation WHERE real_filename = '$name_of_your_file';";
$result = mysql_query($query,$dblink) or die("Query failed.");
$row = mysql_fetch_array($result);
$link = "<a href='download.php?file=" . $row[0] . "'>Click to get file</a>";
echo $link;
Then on your download page:

Code: Select all

$file = $_GET['file'];
$dblink = mysql_connect("hostname","username","password") or die("Could not connect to the database.");
mysql_select_db("your_database",$dblink) or die("Could not select the catalog");
$file = mysql_real_escape_string($file);
$query = "SELECT real_filename FROM file_name_obfuscation WHERE fake_filename = '$file';";
$result = mysql_query($query,$dblink) or die("Query failed.");
$row = mysql_fetch_array($result);
$data = file_get_contents($row[0]) or die('Unable to retrieve file.'); // get the data from the local file to maintain obfuscation, rather than doing a redirect.
echo $data;
I'm happy to answer any questions you have about this, post them here and I'll help you through it. Hope this helps, though!
As a side note, you might run into content-type issues. I'll help you address those if they come up, but I'm out for the day! Beer o'clock has just arrived!

-Andy