DeeDee Posted April 24, 2011 Share Posted April 24, 2011 Hi guys I would like to request some help on a problem I am trying to solve. I am trying to download files from a database but it is only reading the file else statement. So I went about creating this code shown here. <?php if (isset($_GET['id'])) { $id = intval($_GET['id']); if ($id <= 0) { die("Connection Failed"); } else { include('connectme.php'); } $query = "SELECT name, type, size, Content FROM Files WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); if ($result) { if (mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); header("Content-length:" . $row['size']); header("Content-type:" . $row['type']); header("Content-Disposition: attachment; name =" . $row['name']); echo $row['Content']; } } else { echo "Invalid ID"; } mysql_free_result($result); } else { echo "Error!!!"; } //mysql_close(); ?> Link to comment Share on other sites More sharing options...
Jonathon Posted April 24, 2011 Share Posted April 24, 2011 What exactly is it returning? Also here: $query = "SELECT name, type, size, Content FROM Files WHERE id = '$id'"; You don't need to quote the $id as its an integer not a string 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 24, 2011 Author Share Posted April 24, 2011 What exactly is it returning? Also here: $query = "SELECT name, type, size, Content FROM Files WHERE id = '$id'"; You don't need to quote the $id as its an integer not a string It should return the contents of the file and prompt you with a download. Link to comment Share on other sites More sharing options...
Jonathon Posted April 24, 2011 Share Posted April 24, 2011 No, what response is it returning when you run it. but it is only reading the file else statement Which else statement?? And are you sure this line is correct header("Content-Disposition: attachment; name =" . $row['name']); 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 24, 2011 Author Share Posted April 24, 2011 No, what response is it returning when you run it. Which else statement?? And are you sure this line is correct header("Content-Disposition: attachment; name =" . $row['name']); Ah byp, the final else from the first if statement is returned 'Error!!!' I was sure that this line header("Content-Disposition: attachment; name =" . $row['name']); was correct, however I don't have the appropiate book to learn from just the internet and some planning on my part. If there is a book that you can recommend me to learn more about downloading blobs via database I would be greatful. Thanks so far for your replys so far Jonathon. Link to comment Share on other sites More sharing options...
Jonathon Posted April 24, 2011 Share Posted April 24, 2011 This is from the manual // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); Your line doesn't seem to be terminated properly. So basically your $_GET request is failing? it loks to me? Have you tried echoing out the $_GET['id];, what does it return? 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted April 24, 2011 Share Posted April 24, 2011 <?php $query = "SELECT name, type, size, Content FROM Files WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); if ($result) { if (mysql_num_rows($result) == 1) { // if it num_rows = 1 } else { // if not } mysql_free_result($result); } else { // error } ?> First of, it seem you have one to many brackets in your code, after the $result if. Sorry. Just edited out some code before looking at it... From php.net: Mysql_query() For Select(...) mysql_query() returns a [b]resource on success[/b], or [b]FALSE on error[/b]. I don't know if if ($result) will work then. Maybe you should do if ($result !== false) {} instead? I don't know if this is the problem. 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 24, 2011 Author Share Posted April 24, 2011 <?php $query = "SELECT name, type, size, Content FROM Files WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); if ($result) { if (mysql_num_rows($result) == 1) { // if it num_rows = 1 } else { // if not } mysql_free_result($result); } else { // error } ?> First of, it seem you have one to many brackets in your code, after the $result if. Sorry. Just edited out some code before looking at it... From php.net: Mysql_query() For Select(...) mysql_query() returns a [b]resource on success[/b], or [b]FALSE on error[/b]. I don't know if if ($result) will work then. Maybe you should do if ($result !== false) {} instead? I don't know if this is the problem. I have tried it, still does not work. Thanks though it is appriciated. Link to comment Share on other sites More sharing options...
DeeDee Posted April 24, 2011 Author Share Posted April 24, 2011 This is from the manual // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); Your line doesn't seem to be terminated properly. So basically your $_GET request is failing? it loks to me? Have you tried echoing out the $_GET['id];, what does it return? I have restructed the code to grab the output and it returns 1 and 2. I will make adjustments to my code and let you know of my progress soon. Thanks Jon Link to comment Share on other sites More sharing options...
DeeDee Posted April 24, 2011 Author Share Posted April 24, 2011 Hi Guys just to update I managed to have the file to be downloaded however it does not display the contents of the file I will display my new code here <?php ob_start(); include('connectme.php'); $query = "SELECT name, type, size, Content FROM Files"; $result = mysql_query($query) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $read = $row['name']; header("Content-length:" . $row['size'] . "<br>"); header("Content-type:" . $row['type'] . "<br>"); header("Content-Disposition: attachment; filename =" . basename($read) . "<br>"); echo $row['Content'] . "<br>"; readfile($read); } ob_clean(); //exit(); mysql_close(); ob_flush(); ?> Tips are appricated thank you Link to comment Share on other sites More sharing options...
HartleySan Posted April 25, 2011 Share Posted April 25, 2011 What exactly do you want to do? Do you have extire text files stored as blobs in the database? If that's the case, just echo the appropriate information that is grabbed from the database. Otherwise, you'll need to use functions like fread() to actually read external text files, and display the content on the screen. 1 Link to comment Share on other sites More sharing options...
DeeDee Posted April 25, 2011 Author Share Posted April 25, 2011 What exactly do you want to do? Do you have extire text files stored as blobs in the database? If that's the case, just echo the appropriate information that is grabbed from the database. Otherwise, you'll need to use functions like fread() to actually read external text files, and display the content on the screen. Yeah I do have files stored as blobs in the database but when I echo out its contents nothing is returned. I have already written data in the textfile but nothing is returned. I don't understand what I have done wrong but I'll have another look on fread to see if it works. Link to comment Share on other sites More sharing options...
HartleySan Posted April 25, 2011 Share Posted April 25, 2011 I'm not too familiar with outputting files, but I think once you output a file like that, you cannot echo any new information out to the screen. I could be wrong about that though. Either way, a good way to test it would be to first remove all the header info used for downloading the file, and just try to output the content to the screen. Once you have that working, try adding the download layer on after the content is output to the screen. Please let us know how that goes. 1 Link to comment Share on other sites More sharing options...
Larry Posted April 25, 2011 Share Posted April 25, 2011 A single script can only send a single file. You cannot do what you're doing with your while loop. Link to comment Share on other sites More sharing options...
HartleySan Posted April 26, 2011 Share Posted April 26, 2011 Yes, you're right, Larry. I recall making a similar topic, and Larry getting rather annoyed with my persistence to handle multiple file downloads in one script. It cannot be done, in the sense that header information can only be sent once per page. Link to comment Share on other sites More sharing options...
DeeDee Posted May 4, 2011 Author Share Posted May 4, 2011 Hey guys just to give an update that I have managed to download the file with the contents, basically I used this piece of code mysql_fetch_object(); to accomplish my task. I'll provide the link here should anyone needs it in future. http://www.w3schools.com/php/func_mysql_fetch_object.asp My humble thanks to the people who helped and replied. Link to comment Share on other sites More sharing options...
HartleySan Posted May 4, 2011 Share Posted May 4, 2011 Perhaps I never understand well what you wanted to do, but this seems unnecessary. Nevertheless, good work! Link to comment Share on other sites More sharing options...
DeeDee Posted June 6, 2011 Author Share Posted June 6, 2011 Perhaps I never understand well what you wanted to do, but this seems unnecessary. Nevertheless, good work! Thanks HS I am sorry if I have mistreated your understanding in anyway. I can send an update of the written code to provide a example to anyone who wishes to learn about downloading blogs if that is okay with Larry Link to comment Share on other sites More sharing options...
Larry Posted June 6, 2011 Share Posted June 6, 2011 The mysql_fetch_object() function is just an alternative way of fetching a record from the database (an alternative to mysql_fetch_array()). It isn't pertinent to downloading blobs, for the record. But please feel free to share your code. Link to comment Share on other sites More sharing options...
Recommended Posts