Jump to content

PHP download from mysql problems


shane201980

Recommended Posts

    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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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. :shrug:

 

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!)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.