dan09 Posted August 3, 2011 Share Posted August 3, 2011 On some website (not sure which ones) they have a id for each file to download E.G: http://domain.com/download.php?id=30 I would like to create somthing like this but not sure how. I think this will need a database aswell to work Could you help me out? Thanks Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 create a database with 2 fields for sure: id: Auto incremented unique id (a.k.a. 30 from your example) file_location: the location of the file to download. next you need need to make a page that gets that id and queries the database for the correct file. next, to download the file just use the first example found here: readfile That should get you started. When you have some questions post your code with your questions. Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 next you need need to make a page that gets that id and queries the database for the correct file. Ok i have made the database how would i go about creating a file to query the database? Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 <?php // Make a MySQL Connection mysql_connect("localhost", "username", "Password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM download") or die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry echo "Id: ".$row['id']; echo " File: ".$row['file_location']; ?> Ok i have this to query my database it works Could you suggest any other moddifications where needed please? The next thing is to use that example you suggested to me. Im not sure how i would use it. Could you help me with this please? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 first off, you query selects everything from the database, you want to just get one item using the passed in value Your query should look like this: $id = (int)$_GET['id']; $result = mysql_query("SELECT * FROM download where id = $id") Next you will add this to the end of this file: $file = $row['file_location']; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 first off, you query selects everything from the database, you want to just get one item using the passed in value Your query should look like this: $id = (int)$_GET['id']; $result = mysql_query("SELECT * FROM download where id = $id") Could you show me where abouts this would go in the script? Sorry im new to php and phpmyadmin Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 Replace this line: $result = mysql_query("SELECT * FROM download") those two lines I gave you Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 Ok i have added all the script into the file and it looks like this: <?php // Make a MySQL Connection mysql_connect("localhost", "username", "Password") or die(mysql_error()); mysql_select_db("databasr") or die(mysql_error()); // Retrieve all the data from the "example" table $id = (int)$_GET['id']; $result = mysql_query("SELECT * FROM download where id = $id") or die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry echo "Id: ".$row['id']; echo " File: ".$row['file_location']; $file = $row['file_location']; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } ?> When i load the script (download.php) All that is displayed is: Id: File: No data Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 Any ideas into what the next set is? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 delete or comment out: echo "Id: ".$row['id']; echo " File: ".$row['file_location']; if the file exists, it should start the download process, if it doesn't exist it won't do anything, so if nothing happens, you have the wrong path for your file. you need to access the page like this: http://yoursite.com/download.php?id=1 Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 Ok it kind of works i browers: domain.com/download.php?=id1 The output is just text displaying the id and the location of the file I would like it to start downloading not display the location of the file Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 A: your url is wrong B: Re-read my previous post C: After fixing your code post it Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 A: your url is wrong B: Re-read my previous post C: After fixing your code post it A) the url is correct i have tested it out B) i forgot to delete echo of file and id. Now nothing is displayed and no download stating. C) still not fixed Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 First off, you posted this as your url: domain.com/download.php?=id1 Which is wrong, you want this: domain.com/download.php?id=1 Second you have the wrong file location in your database, that is why the file is not downloading. So if your file is located in a sub directory of the current directory (the one that contains download.php) and the directory is called "files" you would have this as the value "files/image1.jpg" in your database. Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 Sorry i meant to put :domain.com/download.php?id=1 On the database i have changed where the file is stored. But still not working Im not sure whey its not working Here is my script: <?php // Make a MySQL Connection mysql_connect("localhost", "username", "Password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); // Retrieve all the data from the "example" table $id = (int)$_GET['id']; $result = mysql_query("SELECT * FROM download where id = $id") or die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry $file = $row['file_location']; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="file.zip"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); ob_clean(); flush(); exit; } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 that means your filename in your database is still wrong. Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 Ok so how should my database look like? 1) /files/file.zip 2) domain.com/files/file.zip Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 it should look more like #1, but I don't know where your download.php file is, and where your files are located. your #1 says that the file is located in the root of the computer (not the web server's public www) in a directory called files. is that the correct location? Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 The file is currently located at: test/download.php I would to download a file stored at : test/files/music.mp3 Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 3, 2011 Share Posted August 3, 2011 so then you would have this as the file_location in your database: test/files/music.mp3 Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 3, 2011 Author Share Posted August 3, 2011 It is still not working All i get is a blank screen Quote Link to comment Share on other sites More sharing options...
dan09 Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks mate i got it working in the end. Great help and advice Thanks Quote Link to comment 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.