Some PHP and My SQL Problem with linking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Alex-B
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 5:04 pm

Some PHP and My SQL Problem with linking

Post by Alex-B »

I need to hide a real file name by using random names that will refer visitor to the actual folder directory were the original file is located.

What I am trying to create is:

Download Now link on one page that will look like this -<a href=”http://www.yourdomain.com/dl.php?urlid=absdef”>Download Now</a> and like
this- http://www.yourdomain.com/fileDownload/ ... myFile.zip or like this - http://www.yourdomain.com/fileDownload/myFile.zip

I have been told that I need to create a table in My SQL DB that will contain urlid along with Original File Names and random names assigned to each file.

In my understanding it will probably look like the table below:

URL ID Original File Name Random File Name
001 Personal-budget-calculator.xls Personalbudget
002 Home-budget.xls Household
003 Purchase-order.xls purchaseorder

How to do it all this is my question?

I believe that this is something that can be done since I have seen on many other web sites furthermore this can also be used to hide your affiliate ID links under some random name in urls.
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: Some PHP and My SQL Problem with linking

Post by turbolemon »

I created something similar for a file manager I wrote a few months back. Basically, you will need to query your table for the identifier passed via the url, fopen + fread the file into a variable (or just use file_get_contents()), then set relevant headers for the file to be treated as a download, finally outputting the content to the end-user:

Code: Select all

$dir= "/path/to/directory/"; //Substitute with the absolute path to the directory containing the files for download
$filename = "file.zip"; //Substitute the filename with the result of your query of the database
 
$data = file_get_contents($dir.$file,FILE_BINARY);
 
header("Content-Disposition: attachment; filename='{$filename}'");
header("Content-Type: application/octet-stream");
 
echo $data;
Alex-B
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 5:04 pm

Re: Some PHP and My SQL Problem with linking

Post by Alex-B »

Thanks for this turbolemon!!

I think this should be it? There is another question than, what if I do not whant to show the name of the actual file in the following url
http://www.yourdomain.com/fileDownload/ ... myFile.zip and make and make it look like http://www.yourdomain.com/fileDownload/ ... eothername, were someother name is the name associated with the actual file in the files directory on the server under the same ID?

Thank again
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: Some PHP and My SQL Problem with linking

Post by turbolemon »

You would ideally use the id in the download link, eg:

http://yoursite.com/download.php?id=1 (where id is the id of the file entry in the database)

Then you would lookup the database row by id something like this..

SELECT * FROM downloads WHERE id = 1 (Substituting 1 for the id provided in the URL above)

..and use the filename provided as a result of the query. This would make the file identifier unique.

$filename = $result['filename'];
Alex-B
Forum Newbie
Posts: 6
Joined: Wed Jul 22, 2009 5:04 pm

Re: Some PHP and My SQL Problem with linking

Post by Alex-B »

Thanks this was really helpful!!
Post Reply