Uploading files into a MySQL database using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ririe
    New Member
    • Jun 2009
    • 20

    #31
    show me your codes for upload

    Comment

    • roseple
      New Member
      • Jun 2009
      • 26

      #32
      Oki here's my code.
      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>
      <FORM METHOD="post" ACTION="add_file.php" ENCTYPE="multipart/form-data">
      <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000">
      <INPUT TYPE="hidden" NAME="action" VALUE="Upload">
       <TABLE BORDER="1" cellspacing="1" cellpadding="3">
        <TR>
         <TD>Subject: </TD>
         <TD><input name="subjects" size = "66%" /></TD>
        </TR>
        
        <TR>
         <TD>Client Name: </TD>
         <TD><input name="clientname" size = "35%" id="clientname" /></TD>
        </TR>
        
        <TR>
         <TD>File: </TD>
         <TD><INPUT type="file" NAME="uploaded_file"></TD>
        </TR>
        <TR>
         <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD>
        </TR>
       </TABLE>
      </FORM>
      
      
      
      <?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 = mysql_connect("localhost", "root", "777") or die (mysql_error());
      		mysql_select_db("filestorage", $dbLink) or die(mysql_error());
      		
              /*if(mysql_connect()) {
                  die("MySQL connection failed: ". mysql_error());
              }*/
       
              # Gather all required data
              $name = mysql_real_escape_string($_FILES['uploaded_file']['name']);
              $mime = mysql_real_escape_string($_FILES['uploaded_file']['type']);
              $size = $_FILES['uploaded_file']['size'];
              $data = mysql_real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
       
              # Create the SQL query
              $query = "
                  INSERT INTO file_upload (
                      FileName, FileMime, FileSize, FileData, subjects, clientname, Created
                  )
                  VALUES (
                      '{$name}', '{$mime}', {$size}, '{$data}', '{$subjects}','{$clientname}', NOW()
                  )";
       
              # Execute the query
              $result = mysql_query($query, $dbLink);
       
              # Check if it was successfull
              if($result) 
              {
                  echo "Success! Your file was successfully added!";
              }
              else 
              {
                  echo "Error! Failed to insert the file";
                  echo "<pre>". mysql_error($dbLink) ."</pre>";
              }
          }
          else 
          {
              echo "Error! 
                      An error accured while the file was being uploaded. 
                      Error code: ". $_FILES['uploaded_file']['error'];
          }
       
          # Close the mysql connection
          mysql_close($dbLink);
      }
      
      # Echo a link back to the mail page
      
      echo "<p><a href='list.php'>Click here to see all Files.</a></p>"; 
      ?>
      </body>
      </html>
      Last edited by Atli; Jun 10 '09, 07:57 AM. Reason: Added [code] tags.

      Comment

      • ririe
        New Member
        • Jun 2009
        • 20

        #33
        I already try your code but there is nothing wrong with it and I can upload the pdf file. What is actually the error come out??

        Comment

        • roseple
          New Member
          • Jun 2009
          • 26

          #34
          Error! An error accured while the file was being uploaded. Error code: 2
          Notice: Undefined variable: dbLink in c:\Inetpub\wwwr oot\uploadingfi les\add_file.ph p on line 86

          Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in c:\Inetpub\wwwr oot\uploadingfi les\add_file.ph p on line 86

          Comment

          • ririe
            New Member
            • Jun 2009
            • 20

            #35
            what is the code on line 82??

            Comment

            • ririe
              New Member
              • Jun 2009
              • 20

              #36
              sorry what is the code on line 86??

              Comment

              • roseple
                New Member
                • Jun 2009
                • 26

                #37
                mysql_close($db Link); //line 86

                Comment

                • roseple
                  New Member
                  • Jun 2009
                  • 26

                  #38
                  That warning and error only appear if pdf file was being uploaded.

                  Comment

                  • ririe
                    New Member
                    • Jun 2009
                    • 20

                    #39
                    As I remember you said that you are using mysql instead of mysqli..
                    I think you should change and use mysql as I believe mysql does not support dblink. So better you change all to mysqli..Try it first

                    Comment

                    • ririe
                      New Member
                      • Jun 2009
                      • 20

                      #40
                      Sorry I mean you should use mysqli as mysql does not support dblink

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #41
                        ririe and roseple,

                        Please use &#91;code] tags when you post you code examples.

                        &#91;code] code goes here &#91;/code]

                        Thanks.

                        Comment

                        • ririe
                          New Member
                          • Jun 2009
                          • 20

                          #42
                          Sorry Atli...Next time I will use code tag

                          Comment

                          • Atli
                            Recognized Expert Expert
                            • Nov 2006
                            • 5062

                            #43
                            The problem there is explained here. (Error code #2).

                            Your form has a MAX_FILESIZE field, who's value is to low to upload your files:
                            Code:
                            <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
                            Increase that and it should work again.

                            Also, the error on line #86 has nothing to do with the fact you are using the old mysql extension.
                            It's because you open your database connection inside the if statement, so when the upload fails it skips it and goes right to the else clause without ever opening it, where it tries to close it, obviously failing.

                            Edit,
                            No, on second glance, the mysql_close call is outside the if/else block altogether.
                            Move it inside the if statement and that error is fixed. (The one on line #86, anyways)
                            Last edited by Atli; Jun 10 '09, 08:09 AM. Reason: I'm still asleep I think :P

                            Comment

                            • roseple
                              New Member
                              • Jun 2009
                              • 26

                              #44
                              Sorry Atli.. next time I will use code tag too.

                              Comment

                              • roseple
                                New Member
                                • Jun 2009
                                • 26

                                #45
                                Atli was right, I move the connection of database outside the if statement and the error regrading mysql_close(); was gone.

                                In max_file_size.. I put 1 000 000. And pdf file is already working, but some pdf file didn't work. Maybe because they are a too large file. Is 1000000, the maximum file size I can put?

                                Comment

                                Working...