shane201980 Posted April 17, 2010 Share Posted April 17, 2010 Ok, here's the run down. I have worked all day almost to get this thing to work correctly. I want a download button on one page to send an id to a php page which is attached here, to download a file that is stored with project information in a mysql database. I know this is not the best idea, but I'm stuck this far and my weekend is now devoted to finishing this setup. At first the id was not being forwarded to the attached php, even through the url, so I made it hidden on my first page as a form and a download button to submit to this page. That part works, I did a test page and printed the id which came over correctly. When I run this page, it says "Error! No documents exist for this project". After testing this out, I believe that the query is returning empty, but I'm not sure. Let me give you all the info I can think of and maybe someone can point me in the right direction. Please be specific as I am still learning and most of my brain cells have exploded... Section of the mysql database that stores the file: Name1 - varchar(255), Null=no Size1 - bigint(255), Null=no, Unsigned, Default=0 Type1 - varchar(50), Null=no Plans - longblob, Attributes=Binary The file shows that it has uploaded to the database, when I browse it I see all the information. When I check the error logs I get this: [Fri Apr 16 20:57:16 2010] [error] [client 68.***.***.***] PHP Notice: Trying to get property of non-object in /var/www/vhosts/someplace.com/httpdocs/Documents/download_help.php on line 26, referer: http://someplace.com/Documents/project-ck.php Any other questions I will be glad to supply any information I can. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Jax2 Posted April 17, 2010 Share Posted April 17, 2010 Try this? $query = "SELECT * FROM this_table WHERE id = '$id' "; Quote Link to comment Share on other sites More sharing options...
shane201980 Posted April 17, 2010 Author Share Posted April 17, 2010 Tried that earlier, but I did make the adjustment and ran it again. Same problem, no change. I keep racking my brain. This thing needs to be up by monday morning, and this is the last item to get working. (Yeah I'm stressing!) Quote Link to comment Share on other sites More sharing options...
mattal999 Posted April 17, 2010 Share Posted April 17, 2010 if($result->num_rows > 0) { This (line 26) should be: if(mysql_num_rows($result) > 0) { Quote Link to comment Share on other sites More sharing options...
shane201980 Posted April 17, 2010 Author Share Posted April 17, 2010 mattal999 that is correct... so easy I can't believe I didn't make the connection. It of course has given me a new problem which I am starting to work on. When the download box pops up it wants to download 'Name1' instead of the file name from the database, and the file is nothing but a notepad document that says Plans. I know that it's just printing the echo, so I have to figure out how to make it retrieve the file. So close... if anyone has any more ideas I would be grateful. Meanwhile I'm off to tweak this thing a little more hopefully. Quote Link to comment Share on other sites More sharing options...
mattal999 Posted April 17, 2010 Share Posted April 17, 2010 header("Content-Disposition: attachment; filename=\"". $row['Name1'] ."\""); // This is the fixed line by the way You should enclose the filename in ""s in case of spaces. I'm not sure about the 'Plans' thing. echo $row['Plans']; Should be: ob_clean(); // Read below flush(); // Don't know why these are here, but the PHP manual has it so it must be right readfile($row['Plans']); Quote Link to comment Share on other sites More sharing options...
shane201980 Posted April 17, 2010 Author Share Posted April 17, 2010 ok... enclosing the headers was not liked and returned an unexpected error until i removed them. I'll mess with it a bit more to see if I can adjust that. replacing the echo $row['Plans']; I now get the following errors: [sat Apr 17 12:50:38 2010] [error] [client 98.***.**.***] PHP Warning: readfile() [<a href='function.readfile'>function.readfile</a>]: Unable to access Plans in /var/www/vhosts/someplace.com/httpdocs/Documents/download_help.php on line 38, referer: http://someplace.com/Documents/project-ck.php [sat Apr 17 12:50:38 2010] [error] [client 98.***.**.***] PHP Warning: readfile( Plans) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in /var/www/vhosts/someplace.com/httpdocs/Documents/download_help.php on line 38, referer: http://someplace.com/Documents/project-ck.php I have attached a view of the table that holds the files, you will see that not all rows have files yet but I am only trying to access the bottom one for now. This is all a test phase until I get it all online and then the real thing will go in place. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
shane201980 Posted April 17, 2010 Author Share Posted April 17, 2010 I think I may have found my problem, but I'm not 100% for sure. I looked into my database table and it's only 3.3kb in size. If I export the table to see the file information it says that the file is stored in a tmp/..... area. I don't think it upload. The Name1,Size1,Type1 is all the correct information, but I just don't see where it could be physically. Just checked it out and my disk space is not changing in size... therefore my files cannot be uploading to the database. The only thing that seems to be going to the database is the file information. (This sucks!) Quote Link to comment Share on other sites More sharing options...
shane201980 Posted April 18, 2010 Author Share Posted April 18, 2010 SOLVED... Ok this seemed to be a collective help to get it working, and I appreciate it greatly. My first problem was that my file was not being uploaded because I did not set up move_uploaded_file function. Once that was fixed it started work with the following: mysql_connect('localhost',$user,$pass); @mysql_select_db($database) or die( "Unable to select database!"); $query = "SELECT * FROM my_table WHERE id = {$id}"; $result = mysql_query($query) or die('Error, query failed'); if($result) { // Make sure the result is valid if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); // Print headers header("Content-Type: ". $row['Type1']); header("Content-Length: ". $row['Size1']); header("Content-Disposition: attachment; filename=". $row['Name1']); // Print data ob_clean(); flush(); readfile($row['Plans']); } else { echo 'Error! No documents exist for this project.'; } Much appreciation here, and I hope this can help others out as well. Just a note though, check your php.ini or run phpinfo() to verify your max file handling capabilities. I have already requested a change from my host for: post_max_size - default 8mb (not large enough for my needs) upload_max_filesize - default 2mb (not large enough for most people) max_execution_time - default 30 (listed in seconds/not always enough time) max_input_time - default 60 (listed in seconds/larger files tend to take a little longer) 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.