Hello, I'm trying to get away from showing the absolute url's of my downloads. I have all my download data stored in a mysql db with multiple tables and fields. One of which is called 'id' and set to auto_increment. What code would I need to use to show my users a url like this: http://www.mydomain.com/library/download.php?id=1234 When they click on a link for a file. I have seen it in most CMS's but am having trouble finding it. Regards
First, you need to get the variable. $id = $_GET['id']; Then you inject it into your sql. SELECT * FROM table WHERE id='{$_GET['id']}' Then you need to fetch the data into an array. mysql_fetch_assoc($sql); *This is just an explanation, not a step by step of the full script.
Thank You. I think I'll have enough to go on from there. After all, if someone does it for you all the time, how would we ever learn. Thanks Again!
Sorry to double post. I've used the method you suggested, but i'm not sure on how to get the file to appear as a download. Thats the only bit i'm stuck on. Reagrds
Another option is to get the file name out of the DB, then use a header redirect to the file. <?php ..your db logic getting the file name for $_GET['id'] $filename = ...the above result...; header("Location: http://theurl.to/thefiles/".$filename,true,307); PHP: With this, you could add some logic to also count the number of downloads as well. -Bing
OK so this is what I have so far <?php $id = $_GET['id']; include("mydbconnect.php"); mysql_select_db("my_db", $con); $result = mysql_query("SELECT url FROM downloads WHERE id='{$_GET['id']}'"); $filename = *What Goes Here?* header("Location: http://mydomain.com/download/path/".$filename,true,307); mysql_close($con); ?> Code (markup): Can you advise me on what I'm missing? Thanks
<?php $id = $_GET['id']; include("mydbconnect.php"); mysql_select_db("my_db", $con); // LIMIT it, so less strain on the mysql server $result = mysql_query("SELECT url FROM downloads WHERE id='{$id}' LIMIT 1"); //$filename = *What Goes Here?* $row = mysql_fetch_assoc($result); $filename = $row['url']; header("Location: http://mydomain.com/download/path/".$filename,true,307); mysql_close($con); ?> PHP:
Just a small change I'd make: $id = intval($_GET['id']); PHP: And furthermore, I'd do: readfile($filename); PHP: ... instead, so people can't get the real path of the file. And it can also be before the public_html dir so no one can access without the script.
Where do I input the readfile($filename) into my code? Also Would this script also work for content? E.G. http://www.mydomain.com/content.php?name=About If so would it mask the path to the content?