Uploading files into a MySQL database using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    Hey. So the second file is uploaded without error, is listed in the files list with a download link, but then fails to be downloaded saying the ID is invalid?

    I see no reason why that would be happening with that code.
    Are you sure the image is being uploaded correctly? Have you checked the database entry for it to see if everything is in order?

    Comment

    • Djurres
      New Member
      • Aug 2014
      • 4

      I think there I found the problem. I am not uploading images, but .DOC / .ODT / .PDF files.. Is there a possibility to fix these?

      I have checked the database after I stored some files, and thats working correctly, so there is no problem with the uploading, there only is a problem with get_file.php

      By the way, I have got one table with multiple uploaded files, over 8 files or so, and I still get this error, so after the first upload it isnt working anymore.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        There is nothing in the code that's specific to images, a part from the error message. In fact, the error message is wrong to specify it as an image. - I originally wrong this as an image upload script, but made it more general for the article. I seem to have overlooked rewording the error message.

        It should work fine with any file type. I tested my original code with a bunch of non-image types. The mime type it provides during the download is the same type it gets from the browser during the upload.

        Can you show us the URL of a download link that fails, and the corresponding row in the database?

        Comment

        • Djurres
          New Member
          • Aug 2014
          • 4

          There we go, this is where it goes wrong, with 2 files in the database. As I see the ID is right, even as the ID of the database is right!

          The template plus files.php


          http://revival.huizevisser.nl/rac/get_file.php?id=5 <-- download url


          phpmyadmin


          _______________ ___________

          I think I found the problem.
          Didnt fixed it yet as I don't know how, but when i have 3 .txt documents in a database, it keeps working, BUT, when i add a .doc/.docx file, its gonna mess up and stop working.

          Preview:
          files.php


          on click download


          Add .DOCX file (Jaarverslag RAC 2014.docx)


          Try to download .DOCX or a txt file,


          database:


          Hope this is enough information!
          skype: gekke_dj

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            Might have something to do with the size of the files. The DOCX file in there is significantly larger than the text files.

            Try calling $result->store_result() ; right after the if ($result) in the get_file script.

            I usually prefer PDO over MySQLi, so I'm not certain about this, but according to the MySQLi docs the num_rows function may not work correctly unless you store the result. If there is a size barrier on what it automatically stores and what it doesn't, the larger size of the DOC/DOCX/PDF files may be causing the num_rows function to return 0 even when there is data, thus causing this error.

            Comment

            • bobobobo
              New Member
              • Sep 2014
              • 2

              How can I change the code to look for values in another row?

              Example: row filled with names and when I type website.com/get_file.php?na me=John instead of website.com/get_file.php?id =1

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                When you say "row" I presume you mean "column"? Because otherwise what you said makes no sense.

                This is simply a matter of changing the column name you are comparing the input value to, and treating the input value as a string instead of a number. - Honestly it's a fairly trivial thing to do; I'm not sure exactly how else to describe what needs to happen.

                Have you tried this? If so, what have you tried? If not, then why not?

                Comment

                • bobobobo
                  New Member
                  • Sep 2014
                  • 2

                  Yes :) I fixed that.

                  Now I have another problem. I upload 1 file and I'm able to download it. If I upload another file, I get error wrong image id on both files (even on the one I was able to download previously) :)

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    OK. Not much I can do without seeing the code. However, it's probably best for you if you don't post it in here. This comments thread is very long and uncomfortable for that purpose.

                    Instead, go to the PHP section and post a question there. (The big green button.) Describe the problem and post the code.

                    Comment

                    • nagarjunanarni
                      New Member
                      • Sep 2014
                      • 1

                      Hi Sir.,

                      how to upload word document, edit it and update the same in MySQL database

                      Thanks & Regards

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        I moved rain92's question into its own thread here for further assistance.

                        Comment

                        • rian92
                          New Member
                          • Oct 2014
                          • 5

                          Hi Atli. Lots of thank for the code. But I have bit of problem, it work perfectly when I upload file. But if I did upload anything it become an error. I hope you can help me. It really urgent. once again thank you.
                          Error :
                          An error accured while the file was being uploaded. Error code: 4

                          Comment

                          • Anju uv
                            New Member
                            • Feb 2014
                            • 1

                            how to view this videos

                            Comment

                            • hannasjas
                              New Member
                              • May 2015
                              • 1

                              Hi, I would like to delete the files. How I going to do? Please help. Thanks.

                              Comment

                              • Slaxer13
                                New Member
                                • Jun 2014
                                • 106

                                @Atli
                                I have an issue... If i change the name of the get_file.php in all code and the name of the file itself, when i try to download it always asks for that specific file (get_file.php). Why is this?

                                Comment

                                Working...