View Full Version : creating download link in php

12-20-2003, 05:43 AM
Hi all. i have a quick (and possibly very stupid) question, and was wondering if anyone could help me with this.

In the php script, i have it accessing a mysql database. The database has the name of the file, and the format it is in (jpg, igf, doc, pdf, etc).

The link i want to create would use the file name, and the suffix.

How can i get the script to put in the title and suffix from the database?

title: doc1
format: doc
preferred link: ahref="title.format"

any suggestions?

thanks all,


12-20-2003, 08:23 AM
Looks like you've just about answered your own question.

Supposing you have a mysql database called "db_files", and table called "tbl_files" and....you have the files you placed below, let's say your database has a row with the following info:

item_number: 1
item_title: my_document
item_extension: doc


/* Standard DB Connect */
$dbName = "db_files"; // the database name
$tableName = "tbl_files"; // the table name
$dbServer = "localhost"; // the address of your server
$dbServerPort = "3306"; // the database server port

$dbUserName = "my_username";
$dbPassword = "my_password";

$serverConnection = mysql_connect($dbServer . ":" . $dbServerPort, $dbUserName, $dbPassword)
or die ('Unable to connect to server: ' . mysql_error());

$dbConnection = mysql_select_db($dbName,$serverConnection);

/* Query the table in the database, and generate the link */

$sql_query = "SELECT * FROM '$tableName' WHERE item_number = '1' ";

$result = mysql_query($sql_query);

while ($row = mysql_fetch_assoc($result)) {

$fileWithExtension = $row["item_title"] . "." . $row["item_extension"];

$theLink = "<a href=\"" . $fileWithExtension . "\">" . $fileWithExtension . "</a>";


/* print out the generated link */
print $theLink;


That's really sloppy code, and i'm just typing it off the top of my head, so I make no guarantees, but it *should* work... The output of that would be:

a link, pointing to mydocument.doc, with the link text (what you'd click on), being the word 'mydocument.doc'


Things to consider:
- If you store file names with spaces or special characters, like "this ! is my Documen&t", then you need to rawurlencode the data before you either print it, or store it in the database

-You need to know what link you're pulling. If you want to pull all items out of the database, then place "print $theLink;" before the curly brace "}" above it. Also change "$sql_query" to:

$sql_query = "SELECT * FROM '$tblName'";

That should select EVERYTHING in that table.

Too much info? not enough info? need it simpler than that? More complex? what are you looking for? Do you need the code so it isn't so easily updatable? ie, would you like for me to shrink it down to 4 or 5 lines of code? (I can, but it'll be harder to read..well...maybe ;))


12-20-2003, 09:28 AM
Originally posted by Celtboy ie, would you like for me to shrink it down to 4 or 5 lines of code? (I can, but it'll be harder to read..well...maybe ;))

-Celt [/B]

cough cough :D

12-20-2003, 11:57 AM
how about doing

$filename = "href=\".mysql_result(mysql_query("SELECT CONCAT(title,'. ',format) FROM tblFiles WHERE ID = 1"),0)."\"";

this will work if you already have a mySQL conection open, and filename is the docname field with format being the extension field and ID as the unique identifer. change tblFiles to whatever your table is called et viola.

I can work this into a proper script if you want, basically the idea is if you only want the link, you can just select one field and use the DB to concatenate your strings.

12-21-2003, 02:10 AM
thank you all for the help. I was able to get this figured out, plus some.

If you would like the answer, here it is.

my items in the table were title, category, and format. these were also the names in the php script to get the information to/from the table.

$myrow["title"], $myrow["category"], $myrow["format"]

printf("<a href=\"%s/%s.%s\">link_text</a>", $myrow["category"], $myrow["title"], $myrow["format"]);

In the example above, i created folders pertaining to the different categories used (pictures, family, recipes, etc). i then used the $category as the folder name in the link (the first %s). the other two are kind of self-explanatory after that. :)

Thanks again for the help. yes, i was right there, but i guess i needed some time away from it to look at it anew.