Uploading files into a MySQL database using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lulitta
    New Member
    • Aug 2010
    • 5

    yes it forces download but when i chose to save or open file just empty file opened..!! if i try to open pdf or doc files just empty file opened but it works with plain/text files..but i need it to work with pdf files :$

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      then I can only assume that there might be the headers wrong or the file doesn’t exist at the designated location.

      Comment

      • lulitta
        New Member
        • Aug 2010
        • 5

        hmmm i have checked headers and files..nothing wrong with them..if i have 2 files, pdf and plain text files in the same location..plain text file works but pdf empty page...!!

        thanx Dormilich for trying to help ^_^..any another suggestions!!

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          ..any another suggestions!!
          not without a demo page.

          Comment

          • ropata
            New Member
            • Aug 2010
            • 1

            Great tutorial Atli

            I was wanting to be able to add accompanying text along with uploading the file. I get I'd need to add text fields to the form and in sert new fields in the MySql database that would need populating but I can't figure out how I would gather that data in the add_file.php

            Cheers

            Rob

            Comment

            • Jim Gordon

              Would anyone happen to have a link to the older version of this code which doesnt use mysqli?

              Much appreciated

              Jimmy

              Comment

              • Sagar Joshi

                Hey Guys i have been in PHP coding for about 6 months and also tried out this very fantastic tutorial of downloading and uploading.... well upload works like charm! but so does download but when i download the file such as doc file or image file i am not able to view and image shows that it is not supported and word shows that it is corrupted or unsupported format! pls i need help! thanking you in advance!

                Comment

                • kovik
                  Recognized Expert Top Contributor
                  • Jun 2007
                  • 1044

                  Originally posted by Jim Gordon
                  Would anyone happen to have a link to the older version of this code which doesnt use mysqli?

                  Much appreciated

                  Jimmy
                  There's no reason to use mysql_* functions instead of MySQLi.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    Originally posted by Sagar Joshi
                    Hey Guys i have been in PHP coding for about 6 months and also tried out this very fantastic tutorial of downloading and uploading.... well upload works like charm! but so does download but when i download the file such as doc file or image file i am not able to view and image shows that it is not supported and word shows that it is corrupted or unsupported format! pls i need help! thanking you in advance!
                    Hey.

                    If you are getting corrupted files, a likely reason is that PHP is printing errors or warnings into the file. To see those errors you can simply change line #32 in the get_file.php script so it reads: header('Content-Type: text/html');, and then you need to comment out line #34.

                    This should print the error, along with all the data, into your browser instead of you getting a download dialog. Then you can browse through it and read the errors/warnings being printed.

                    Comment

                    • Kenneth Roggers

                      Thanks

                      Nice Code. Thanks to the original author. May your days be long...

                      Comment

                      • Samuell Ngu
                        New Member
                        • Dec 2010
                        • 1

                        Hi Atli,
                        I you help me change the mysqli to mysql?


                        Code:
                        <?php
                        // Check if a file has been uploaded
                        if(isset($_FILES['uploaded_file'])) {
                            // Make sure the file was sent without errors
                            if($_FILES['uploaded_file']['error'] == 0) {
                                // Connect to the database
                                $dbLink = new mysqli('localhost', 'root', '###', '###');
                                if(mysqli_connect_errno()) {
                                    die("MySQL connection failed: ". mysqli_connect_error());
                                }
                         
                                // Gather all required data
                                $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
                                $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
                                $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
                                $size = intval($_FILES['uploaded_file']['size']);
                         
                                // Create the SQL query
                                $query = "
                                    INSERT INTO `file` (
                                        `name`, `mime`, `size`, `data`, `created`
                                    )
                                    VALUES (
                                        '{$name}', '{$mime}', {$size}, '{$data}', NOW()
                                    )";
                         
                                // Execute the query
                                $result = $dbLink->query($query);
                         
                                // Check if it was successfull
                                if($result) {
                                    echo 'Success! Your file was successfully added!';
                                }
                                else {
                                    echo 'Error! Failed to insert the file'
                                       . "<pre>{$dbLink->error}</pre>";
                                }
                            }
                            else {
                                echo 'An error accured while the file was being uploaded. '
                                   . 'Error code: '. intval($_FILES['uploaded_file']['error']);
                            }
                         
                            // Close the mysql connection
                            $dbLink->close();
                        }
                        else {
                            echo 'Error! A file was not sent!';
                        }
                         
                        // Echo a link back to the main page
                        echo '<p>Click <a href="index.html">here</a> to go back</p>';
                        ?>

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          I you help me change the mysqli to mysql?
                          the mysql_* functions are deprecated, thus it would be a waste of time.

                          Comment

                          • Hasan Baig
                            New Member
                            • Jan 2011
                            • 1

                            I like above code that is very simple and useful
                            but its giving me error on
                            db->close();
                            while uploading the file

                            Comment

                            • Atli
                              Recognized Expert Expert
                              • Nov 2006
                              • 5062

                              Hey Hasan.
                              Which file is it, and what exactly does the error say?

                              Comment

                              • marifard
                                New Member
                                • Aug 2011
                                • 1

                                Hi,

                                Instead of phase 4, can I display the files such as images in a page instead from downloading?

                                Thanks
                                marifard

                                Comment

                                Working...