Uploading files into a MySQL database using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tweeengl
    New Member
    • Apr 2009
    • 4

    #16
    Thanks very much alti for you respond it was great help for me :))

    Comment

    • roseple
      New Member
      • Jun 2009
      • 26

      #17
      Error on downloading files

      Hi, I am a newbie here in bytes forum.

      I exactly follow the whole script you post, from phase 0 to phase 4. Fortunately there was no error and the codes works fine from phase 0 to phase 3. I have a problem in downloading. When I hit the download link, an error occur.

      Here's the error..

      Warning: Cannot modify header information - headers already sent by (output started at c:\Inetpub\wwwr oot\uploadingfi les\ddl.php:10) in c:\Inetpub\wwwr oot\uploadingfi les\ddl.php on line 42

      Warning: Cannot modify header information - headers already sent by (output started at c:\Inetpub\wwwr oot\uploadingfi les\ddl.php:10) in c:\Inetpub\wwwr oot\uploadingfi les\ddl.php on line 43

      Warning: Cannot modify header information - headers already sent by (output started at c:\Inetpub\wwwr oot\uploadingfi les\ddl.php:10) in c:\Inetpub\wwwr oot\uploadingfi les\ddl.php on line 44
      Another error was, there's no download pop window appear.

      What is seem to be the error. Thanks in advance.
      Last edited by Atli; Jun 9 '09, 11:46 AM. Reason: Added the [quote] tags.

      Comment

      • ririe
        New Member
        • Jun 2009
        • 20

        #18
        hi..
        I think you should check the code in the phase 4. Make sure there is no white space before the <?php. It is related to the UTF rules..

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #19
          Hi roseple.

          ririe is right.
          The headers are sent as soon as you start sending content to the browser, at which point you can not alter them (obviously).
          If you try, you get that error.

          Even a single space before your <?php tags will cause this, as will any echo calls from your scripts.

          If there is absolutely no way to avoid sending content before altering the headers, you can use the Output Buffering Control functions to delay sending the content.

          Comment

          • roseple
            New Member
            • Jun 2009
            • 26

            #20
            Thank you Atli and ririe on your reply.

            I just check my codes if there is a white space on my download page. But the error is still the same. It does not raise a file download dialog box.

            I don't know what to edit/debug anymore.
            Thanks guys..

            Comment

            • ririe
              New Member
              • Jun 2009
              • 20

              #21
              Roseple, why don't you attach along your download codes here..
              so that we can together see and check the codes.Then we will try to find
              the errors...

              Comment

              • roseple
                New Member
                • Jun 2009
                • 26

                #22
                Oh.. oki i will attach the file..

                By the way, is mysqli different from mysql?
                If i change all mysqli command to mysql, this can cause any problem?

                Comment

                • ririe
                  New Member
                  • Jun 2009
                  • 20

                  #23
                  MySQLi is designed to work with MySQL 4.1.3 or higher and gives access to more advanced features in MySQL. It requires PHP 5.0 to use. It also gives you more control because you can change MySQL settings using MySQLi without stopping and starting the MYSQL server.
                  MySQLi is also the OOP version of MySQL extension. MySQLi supports some things that the old MySQL extension doesn't. A lot of people still use the original MySQL extension vs the new MySQLi extension because MySQLi requires MySQL 4.1.13+ and PHP 5.0.7+. However, they both provide access MySQL features.

                  Comment

                  • roseple
                    New Member
                    • Jun 2009
                    • 26

                    #24
                    Oh I see. But like me who is a newbie programmer, I still used the original MySql extension. So in above code you gave, I change all the mysqli to mysql. You can see on my attached file.
                    Code:
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <title>Untitled Document</title>
                    </head>
                    <body><?php
                    # Make sure an ID was passed
                    if(isset($_GET['id']))
                    {
                        # Get the ID
                        $id = $_GET['id'];
                     
                        # Make sure the ID is in fact a valid ID
                        if(!is_numeric($id) || ($id <= 0)) {
                            die("The ID is invalid!");
                        }
                     
                        # Connect to the database
                        $dbLink = mysql_connect("localhost", "root", "777") or die (mysql_error());
                    		mysql_select_db("filestorage", $dbLink) or die(mysql_error());
                     
                        # Fetch the file information
                        $query = "
                            SELECT FileMime, FileName, FileSize, FileData 
                            FROM filestorage 
                            WHERE FileID = {$id}";
                     
                        $result = @mysql_query($query,$dbLink)
                            or die("Error! Query failed: <pre>". mysqli_error($dbLink) ."</pre>");
                     
                        # Make sure the result is valid
                        if(mysql_num_rows($result) == 1)
                        {
                            # Get the row
                            $row = mysql_fetch_assoc($result);
                     
                            # Print headers
                            header("Content-Type:".$row['FileMime']);
                            header("Content-Length:".$row['FileSize']);
                            header("Content-Disposition:attachment;filename=".$row['FileName']);
                     
                            # Print data
                            echo $row['FileData'];
                        }
                        else
                        {
                            echo "Error! No image exists with that ID.";
                        }
                     
                        # Free the mysqli resources
                        @mysql_free_result($result);
                        @mysql_close($dbLink);
                     
                    }
                    else
                    {
                        echo "Error! No ID was passed.";
                    }
                    ?>
                    </body>
                    </html>
                    Last edited by Atli; Jun 10 '09, 07:56 AM. Reason: Changed the [quote] tags to [code] tags.

                    Comment

                    • ririe
                      New Member
                      • Jun 2009
                      • 20

                      #25
                      Code:
                      <?php
                      # Make sure an ID was passed
                      if(isset($_GET['id']))
                      {
                      # Get the ID
                      $id = $_GET['id'];
                      
                      # Make sure the ID is in fact a valid ID
                      if(!is_numeric($id) || ($id <= 0)) {
                      die("The ID is invalid!");
                      }
                      
                      # Connect to the database
                      $dbLink = mysql_connect("localhost", "root", "777") or die (mysql_error());
                      mysql_select_db("filestorage", $dbLink) or die(mysql_error());
                      
                      # Fetch the file information
                      $query = "
                      SELECT FileMime, FileName, FileSize, FileData
                      FROM filestorage
                      WHERE FileID = {$id}";
                      
                      $result = @mysql_query($query,$dbLink)
                      or die("Error! Query failed: <pre>". mysqli_error($dbLink) ."</pre>");
                      
                      # Make sure the result is valid
                      if(mysql_num_rows($result) == 1)
                      {
                      # Get the row
                      $row = mysql_fetch_assoc($result);
                      
                      # Print headers
                      header("Content-Type:".$row['FileMime']);
                      header("Content-Length:".$row['FileSize']);
                      header("Content-Disposition:attachment;filename=".$row['FileName']);
                      
                      # Print data
                      echo $row['FileData'];
                      }
                      else
                      {
                      echo "Error! No image exists with that ID.";
                      }
                      
                      # Free the mysqli resources
                      @mysql_free_result($result);
                      @mysql_close($dbLink);
                      
                      }
                      else
                      {
                      echo "Error! No ID was passed.";
                      }
                      ?>

                      Roseple, you just try use the codes above and delete all those thing...

                      -----delete-----
                      Code:
                      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                      <html xmlns="http://www.w3.org/1999/xhtml">
                      <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                      <title>Untitled Document</title>
                      </head>
                      <body>
                      </body>
                      </html>
                      -----delete------

                      The opening for html, head and all the things are not required for this code.As Atli said the headers are sent as soon as you start sending content to the browser, so you can not alter them. If you put the html codes, the program will assume that the header are already sent. So your actually program cannot running properly and you will get errors.
                      Last edited by Atli; Jun 10 '09, 07:56 AM. Reason: Added [code] tags.

                      Comment

                      • roseple
                        New Member
                        • Jun 2009
                        • 26

                        #26
                        Wow, you guys were so great.
                        It's running properly now. The file download dialog box is now present and I can actually download the file.

                        Thank you so much.

                        Comment

                        • ririe
                          New Member
                          • Jun 2009
                          • 20

                          #27
                          Ok..congratulat ion roseple..
                          Good Luck in your work...

                          Comment

                          • roseple
                            New Member
                            • Jun 2009
                            • 26

                            #28
                            Hi, roseple again.. :)

                            If I try to upload an mp3, mpeg and pdf file but an error occurred. Is it because of their file size or the code does not support that said file?

                            Thanks...

                            Comment

                            • ririe
                              New Member
                              • Jun 2009
                              • 20

                              #29
                              well, the code cannot support the mp3 file but it should work for pdf..

                              Comment

                              • roseple
                                New Member
                                • Jun 2009
                                • 26

                                #30
                                Even if I try to increase the maximum file size value, pdf file does not work still.

                                Comment

                                Working...