Uploading files into a MySQL database using PHP

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

    #91
    Hey itsmenarwal.

    Originally posted by itsmenarwal
    ..I am using window vista..
    That would be your first problem ;-)

    Seriously though. I would guess that something - most likely a warning - is being printed with your image data, thus corrupting it.

    Try commenting out the Content-type header. That would make the file data print to the browser window, along with whatever is corrupting it. That way you can spot the error and fix it.

    You can also try just silencing all errors by adding the following to the top of your page:
    [code=php]ini_set('displa y_errors', false);
    error_reporting (0);[/code]

    i posted one problem regarding opening downladed file error(not opening image)..i am not finding it..where it is
    Posts from new members (under 5 posts) go through extra spam-filtering, so they may not always be immediately visible. There should have been a notice about that when you posted it. - In any case, it seems to be visible now.

    Comment

    • itsmenarwal
      New Member
      • Jan 2010
      • 20

      #92
      now check out the error in opening the downloaded file

      @ati

      First of all i want to say thanks to you for such good tutorials.Reall y they help new guies very much.I just started php i found this link for uploading files..It is really appriciable.Kee p it up..Kepp going bro...


      I dont know much about headers but from ur post i cam to know that to see the data on client browser server has to send some inforamtion with the file which tell its type etc..The headers which i used are..

      Code:
      header("Content-Type: ". $row['mime']);
      header("Content-Length: ". $row['size']);
       header("Content-Disposition: attachment; filename=". $row['name']);
      This time i tried to open a pdf file rather than jpeg file.I have acrobat reader installed in my system.


      And Now it is showing error that ""Adobe reader can not open 'ajex.pdf because it is either not supported file type or beacuse the file has been damaged.It was sent as email attachment and was not corectly decoded"

      I also tried silencer code at the top but below <?php but nothing happening..
      Now tell me friends how i can overcome from this problem
      Last edited by Atli; Jan 25 '10, 07:07 AM. Reason: Added [code] tags.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #93
        Try commenting out the headers.
        [code=php]/*header("Conten t-Type: ". $row['mime']);
        header("Content-Length: ". $row['size']);
        header("Content-Disposition: attachment; filename=". $row['name']);*/[/code]
        And then open any file.

        This should spill all the raw data for the file into the browse window as text. If there is indeed an error corrupting it, it should display with the data. Then it is just a matter of correcting it.

        If that turns out not to be the problem, it gets a little more complicated.

        Comment

        • itsmenarwal
          New Member
          • Jan 2010
          • 20

          #94
          i Tried to comment the above headers -> no error is displaying by doing this simply xampp.imagename (just like temp name is displaying..Sti ll i can download the file but file is not opening..Please help

          Comment

          • RomeoX
            New Member
            • Jan 2010
            • 25

            #95
            hi there.

            Could you please Atli fix my code. I tried to change your code from mysqli to mysql but I couldn't edited. there is a problem syntax error.

            Here is the code for download page
            Code:
            <?php 
            // Connect to the database 
            $dbLink = mysql_connect("localhost", "root", "") or die (mysql_error()); 
            mysql_select_db("library", $dbLink) or die(mysql_error()); 
              
            // Query for a list of all existing files 
            $sql = 'SELECT id, name, mime, size, FROM pdf2'; 
            
            $result = mysql_query($sql,$dbLink) 
            or die("Error! Query failed: <pre>". mysql_error($dbLink) ."</pre>"); 
            
            
            // Check if it was successfull 
            if($result) {
            if(mysql_num_rows($result) == 0) {
                    echo '<p>There are no files in the database</p>'; 
            		}
            else { 
            # Get the row 
            $row = mysql_fetch_assoc($result); 
             
                    // Print the top of a table 
                    echo '<table width="100%"> 
                            <tr> 
                                <td><b>Name</b></td> 
                                <td><b>Mime</b></td> 
                                <td><b>Size (bytes)</b></td> 
                                <td><b>Created</b></td> 
                                <td><b>&nbsp;</b></td> 
                            </tr>'; 
              
                    // Print each file 
                    while($row = $result->fetch_assoc()) { 
                        echo " 
                            <tr> 
                                <td>{$row['name']}</td> 
                                <td>{$row['mime']}</td> 
                                <td>{$row['size']}</td> 
                                <td>{$row['created']}</td> 
                                <td><a href='get_file.php?id={$row['id']}'>Download</a></td> 
                            </tr>"; 
                    } 
              
                    // Close table 
                    echo '</table>'; 
                } 
              
                // Free the result 
            @mysql_free_result($result); 
            } 
            else 
            { 
                echo 'Error! SQL query failed:'; 
                echo "<pre>{$dbLink->error}</pre>"; 
            } 
              
            // Close the mysql connection 
            @mysql_close($dbLink); 
            ?>

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #96
              Hey RomeoX.

              If you need help debugging an error message, please include the error message in the post. That way we don't have to guess what it is.

              However, your error is likely on line #34. The result of a mysql_query call is not an object. You can not use method calls on it like you can on the result object of a mysqli::query call. You should be using the mysql_fetch_ass oc function instead.

              Comment

              • RomeoX
                New Member
                • Jan 2010
                • 25

                #97
                Hi Atli, You are totally right. Now it's working fine. I changed the things that u told me about. Thanks a million.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #98
                  Glad to hear that, RomeoX :-)

                  Originally posted by itsmenarwal
                  i Tried to comment the above headers -> no error is displaying by doing this simply xampp.imagename (just like temp name is displaying..Sti ll i can download the file but file is not opening..Please help
                  Sorry to leave you hanging like that. I blame IE. (For no particular reason... It just seems fitting :P)

                  Anyways, if you are still having this problem, browsers usually print the image name when they receive an image that is corrupt. - What you need to do is find out why it is corrupted, and to do that you need the browser to display the image data as text. - That you can do by hunting down all header() calls in your code; comment them out or remove them, and add a single "header('conten t-type: text/plain');" anywhere before the image is printed.

                  Once you can view the image data as text, the warning/error (if that is indeed teh problem) should be clearly visible among the output. (It should be either before or after all the random characters).

                  Note, IE seems to have a problem with "text/plain" so you should do this using any other browser. (Firefox is excellent for this kind of stuff... and all kinds of stuff, really.)

                  Comment

                  • santhanalakshmi
                    New Member
                    • May 2009
                    • 147

                    #99
                    hi,

                    I am facing a problem in phase 2.

                    Fatal error: Call to a member function real_escape_str ing() on a non-object in C:\xampp\htdocs \add_file.php on line 13


                    What should be my text file format for uploading ?

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      the function is called mysql_real_esca pe_string()

                      Comment

                      • santhanalakshmi
                        New Member
                        • May 2009
                        • 147

                        hi,

                        Its uploading to my database.Thanks its working fine.

                        Comment

                        • mattgnr
                          New Member
                          • May 2010
                          • 5

                          Hi,

                          I am having a slight problem when downloading the file.

                          E.g. the file upload is called Unit 1 Lesson 2.pdf. When i go to download the file, if i choose the 'open' option, Adobe opens fine. If i click on 'save' however, it just saves the file as 'Unit' without the rest of the name and no extension.

                          Any ideas?

                          Follow Up: I have noticed that this is only a problem in Firefox. Works perfectly in IE.

                          My code to display can be found below:

                          Code:
                          <?php
                          // Make sure an ID was passed
                          if(isset($_GET['id'])) {
                          // Get the ID
                              $id = intval($_GET['id']);
                           
                              // Make sure the ID is in fact a valid ID
                              if($id <= 0) {
                                  die('The ID is invalid!');
                              }
                              else {
                                  // Connect to the database
                                  $dbLink = new mysqli('127.0.0.1', '####', '#####', '#####');
                                  if(mysqli_connect_errno()) {
                                      die("MySQL connection failed: ". mysqli_connect_error());
                                  }
                           
                                  // Fetch the file information
                                  $query = "
                                      SELECT `mime`, `name`, `size`, `data`
                                      FROM `file`
                                      WHERE `id` = {$id}";
                                  $result = $dbLink->query($query);
                           
                                  if($result) {
                                      // Make sure the result is valid
                                      if($result->num_rows == 1) {
                                      // Get the row
                                          $row = mysqli_fetch_assoc($result);
                           
                                          // Print headers
                                          header("Content-Type: ". $row['mime']);
                                          header("Content-Length: ". $row['size']);
                                          header("Content-Disposition: attachment; filename=". $row['name']);
                           
                                          // Print data
                                          echo $row['data'];
                                      }
                                      else {
                                          echo 'Error! No image exists with that ID.';
                                      }
                           
                                      // Free the mysqli resources
                                      @mysqli_free_result($result);
                                  }
                                  else {
                                      echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
                                  }
                                  @mysqli_close($dbLink);
                              }
                          }
                          else {
                              echo 'Error! No ID was passed.';
                          }
                          ?>

                          Comment

                          • mirasol
                            New Member
                            • Jul 2010
                            • 6

                            i used the given codes, how to add textbox and it will be save to mysql?

                            Comment

                            • mirasol
                              New Member
                              • Jul 2010
                              • 6

                              Code:
                              <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>Teacher ID: </TD> 
                              	   <TD><input name="subjects" size = "66%" /></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>
                              Last edited by Dormilich; Jul 13 '10, 05:34 AM. Reason: Please use [code] tags when posting code

                              Comment

                              • mirasol
                                New Member
                                • Jul 2010
                                • 6

                                what is the supply codes to list_file.php and add_file.php?

                                Comment

                                Working...