Loading ...
Sorry, an error occurred while loading the content.

Re: problem with downloading file using blob data from d/b

Expand Messages
  • Vinu Thomas
    The following code works for me. To save the file: 0) { $fileName =
    Message 1 of 4 , Aug 3, 2006
    • 0 Attachment
      The following code works for me.
      To save the file:
      <?php


      if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
      {
      $fileName = $_FILES['userfile']['name'];
      $tmpName = $_FILES['userfile']['tmp_name'];
      $fileSize = $_FILES['userfile']['size'];
      $fileType = $_FILES['userfile']['type'];

      $fp = fopen($tmpName, 'r');
      $content = fread($fp, filesize($tmpName));
      $content = addslashes($content);
      fclose($fp);

      if(!get_magic_quotes_gpc())
      {
      $fileName = addslashes($fileName);
      }

      $conn = mysql_connect('localhost', 'root', '');
      if (!$conn) {
      die('Could not connect: ' . mysql_error());
      }
      mysql_select_db('blob');
      $query = "INSERT INTO test (name, size, type, file ) ".
      "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

      mysql_query($query) or die(mysql_error());

      echo "<br>File $fileName uploaded<br>";
      }?>

      To retreive the file I modified your code:

      <?php
      $id = $_GET["id"];
      $conn = mysql_connect('localhost', 'root', '');
      if (!$conn) {
      die('Could not connect: ' . mysql_error());
      }
      mysql_select_db('blob');
      $result = mysql_query("select * from test where id=$id") or die
      (mysql_error());
      $row = mysql_fetch_array($result);

      if ($row["type"] == "application/pdf")
      $file_name = $row["name"] . ".pdf";
      else if ($row["type"] == "application/doc" or $row["type"] ==
      "application/msword")
      $file_name = $row["name"] . ".doc";
      else if ($row["type"] == "application/mpp")
      $file_name = $row["name"] . ".mpp";

      $file_data = $row['file'];
      $file_type = $row['type'];
      $file_size = $row['size'];

      header("Content-type:$file_type");
      header("Content-length:$file_size");
      header('Content-Disposition: attachment; filename="' . $file_name . '"');
      echo $file_data;
      ?>

      Regards,
      Vinu

      --- In bang-phpug@yahoogroups.com, bhavna patel <bhavna_coolinus@...>
      wrote:
      >
      > I have tried that also using stipslashes but still not working...
      > If u have any examples can i see it?
      >
      > Vinu Thomas <vinuthomas@...> wrote:
      I see that you're adding slashes while saving the data into the
      > database. I guess that if you strip slashes while retreiving the data,
      > it should sort out the problem.
      >
      > In most cases, if save the file and open the contents in a text
      > editor, you'll see some PHP error message thrown up in file.
      >
      > Regards,
      > Vinu
      >
      > --- In bang-phpug@yahoogroups.com, "bhavna_coolinus"
      > <bhavna_coolinus@> wrote:
      > >
      > > Hi friends,
      > >
      > > I am getting "Read-Error. Error Reading File" error while
      downloading
      > > blob data from data... i am inserting file as pdf,doc,txt data in
      > > database as asci value. while downloading from front i am getting
      this
    Your message has been successfully submitted and would be delivered to recipients shortly.