schalk Posted January 24, 2009 Share Posted January 24, 2009 Hello Everyone, I am busy working on a project which needs to be able to download a file from a URL which has a certain fileID. For example if someone does "http://www.mysite.com/download.php?fileID=1" then it does exactly the same as if you were to go to "http://www.mysite.com/download/file1.zip" and if instead you did "http://www.mysite.com/download.php?fileID=2" then it would do exactly the same as "http://www.mysite.com/download/file2.zip". How do I achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/142295-downloadable-file/ Share on other sites More sharing options...
DeanWhitehouse Posted January 24, 2009 Share Posted January 24, 2009 use $_GET to get the id then get the file name from the db, then send the file as a header e.g. $id = $_GET['id']; $sql = mysql_query("SELECT filename FROM table WHERE id = '".mysql_real_escape_string($id)."' LIMIT 1") or die(mysql_error()); $sql = mysql_fetch_assoc($sql); header("Location:".$sql['filename']); Quote Link to comment https://forums.phpfreaks.com/topic/142295-downloadable-file/#findComment-745571 Share on other sites More sharing options...
schalk Posted January 24, 2009 Author Share Posted January 24, 2009 use $_GET to get the id then get the file name from the db, then send the file as a header e.g. $id = $_GET['id']; $sql = mysql_query("SELECT filename FROM table WHERE id = '".mysql_real_escape_string($id)."' LIMIT 1") or die(mysql_error()); $sql = mysql_fetch_assoc($sql); header("Location:".$sql['filename']); Ahh so header(); was the magic function I was looking for, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142295-downloadable-file/#findComment-745575 Share on other sites More sharing options...
DeanWhitehouse Posted January 24, 2009 Share Posted January 24, 2009 That might just redirect to the file page, or it might not i am not %100. This is my code for downloading zip files. <?php if(isset($_GET['download']) && is_numeric($_GET['download'])) { $id = $_GET['download']; $id = stripslashes($id); $sql = "SELECT * FROM djw_snippet WHERE id = ".$id; $sql = mysql_query($sql); $count = mysql_num_rows($sql); if($count == 0) { header("Location:http://djw-webdesign.awardspace.com/snippet.php"); } else { $row = mysql_fetch_assoc($sql); $file = $row['file']; if(!file_exists("Files/".$file)) { $sq1 = "INSERT INTO djw_missing_file (file,ip) VALUES ('".$file."','".$_SERVER['REMOTE_ADDR']."')"; $sq1 = mysql_query($sq1); ?> <script type="text/javascript"> alert("The file you are trying to download cannot be found. This error has been reported to an administrator."); </script><noscript> The file you are trying to download cannot be found. This error has been reported to an administrator.<br> You will now be redirected. </noscript> <meta http-equiv="refresh" content="3;url=http://djw-webdesign.awardspace.com/snippet.php"> <?php } else { $downs = $row['downloads'] + 1; mysql_query("UPDATE djw_snippet SET downloads = '$downs' WHERE id = '$id'"); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=".basename($file)); header( "Content-Description: File Transfer"); readfile($file); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142295-downloadable-file/#findComment-745577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.