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

problem with downloading file using blob data from d/b

Expand Messages
  • bhavna_coolinus
    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
    Message 1 of 4 , Aug 1, 2006
    • 0 Attachment
      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
      error... can anybody tell why this error comes..

      to store file data into d/b i am ussing code:

      $file_name = $_FILES["doc"]["tmp_name"];
      $file_type = $_FILES["doc"]["type"];
      $file_size = filesize($file_name);

      $file_data = addslashes(fread(fopen($file_name, "r"),
      filesize($file_name)));

      mysql_query("update PROJ_DOCUMENT set
      DESCRIPTION='$description',PROJECT_ID=$project,MIME_TYPE='$file_type',SIZE=$file_size,"DOC='$file_data',UPLOADED_BY=$_SESSION[SESSION_USER_ID],UPLOADED_DATE=CURDATE(),RECORD_STATUS=$record_status
      where DOC_ID=$id") or die(mysql_error());


      and to download this file I am using:

      <?php
      require_once("includes/config.inc.php");
      $id =$_GET["id"];

      $result = mysql_query("select * from PROJ_DOCUMENT where DOC_ID=$id");
      $row = mysql_fetch_array($result);

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


      $file_data = $row['DOC'];
      $file_type = $row["MIME_TYPE"];
      $file_size = $row["SIZE"];

      $handle=fopen($file_name,"wb");
      fwrite($handle,"$file_data",$file_size);
      fclose($handle);


      header("Content-type:$file_type");
      if(headers_sent())
      echo ('Some data has already been output to browser, can\'t send
      file');
      header("Content-length:$file_size");
      header('Content-Disposition: attachment; filename="'.$file_name.'"');
      echo $file_data;
      unlink($file_name);

      ?>

      please tell me where i should modify.. i am not able to open donloaded
      document bcoz of this Read-Error error
    • Vinu Thomas
      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
      Message 2 of 4 , Aug 1, 2006
      • 0 Attachment
        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
        > error... can anybody tell why this error comes..
        >
        > to store file data into d/b i am ussing code:
        >
        > $file_name = $_FILES["doc"]["tmp_name"];
        > $file_type = $_FILES["doc"]["type"];
        > $file_size = filesize($file_name);
        >
        > $file_data = addslashes(fread(fopen($file_name, "r"),
        > filesize($file_name)));
        >
        > mysql_query("update PROJ_DOCUMENT set
        >
        DESCRIPTION='$description',PROJECT_ID=$project,MIME_TYPE='$file_type',SIZE=$file_size,"DOC='$file_data',UPLOADED_BY=$_SESSION[SESSION_USER_ID],UPLOADED_DATE=CURDATE(),RECORD_STATUS=$record_status
        > where DOC_ID=$id") or die(mysql_error());
        >
        >
        > and to download this file I am using:
        >
        > <?php
        > require_once("includes/config.inc.php");
        > $id =$_GET["id"];
        >
        > $result = mysql_query("select * from PROJ_DOCUMENT where DOC_ID=$id");
        > $row = mysql_fetch_array($result);
        >
        > if($row["MIME_TYPE"] == "application/pdf")
        > $file_name = $row["TITLE"].".pdf";
        > else if($row["MIME_TYPE"] == "application/doc" or $row["MIME_TYPE"]
        > == "application/msword")
        > $file_name = $row["TITLE"].".doc";
        > else if($row["MIME_TYPE"] == "application/mpp")
        > $file_name = $row["TITLE"].".mpp";
        >
        >
        > $file_data = $row['DOC'];
        > $file_type = $row["MIME_TYPE"];
        > $file_size = $row["SIZE"];
        >
        > $handle=fopen($file_name,"wb");
        > fwrite($handle,"$file_data",$file_size);
        > fclose($handle);
        >
        >
        > header("Content-type:$file_type");
        > if(headers_sent())
        > echo ('Some data has already been output to browser, can\'t send
        > file');
        > header("Content-length:$file_size");
        > header('Content-Disposition: attachment;
        filename="'.$file_name.'"');
        > echo $file_data;
        > unlink($file_name);
        >
        > ?>
        >
        > please tell me where i should modify.. i am not able to open donloaded
        > document bcoz of this Read-Error error
        >
      • bhavna patel
        I have tried that also using stipslashes but still not working... If u have any examples can i see it? Vinu Thomas wrote:
        Message 3 of 4 , Aug 1, 2006
        • 0 Attachment
          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
          > error... can anybody tell why this error comes..
          >
          > to store file data into d/b i am ussing code:
          >
          > $file_name = $_FILES["doc"]["tmp_name"];
          > $file_type = $_FILES["doc"]["type"];
          > $file_size = filesize($file_name);
          >
          > $file_data = addslashes(fread(fopen($file_name, "r"),
          > filesize($file_name)));
          >
          > mysql_query("update PROJ_DOCUMENT set
          >
          DESCRIPTION='$description',PROJECT_ID=$project,MIME_TYPE='$file_type',SIZE=$file_size,"DOC='$file_data',UPLOADED_BY=$_SESSION[SESSION_USER_ID],UPLOADED_DATE=CURDATE(),RECORD_STATUS=$record_status
          > where DOC_ID=$id") or die(mysql_error());
          >
          >
          > and to download this file I am using:
          >
          > <?php
          > require_once("includes/config.inc.php");
          > $id =$_GET["id"];
          >
          > $result = mysql_query("select * from PROJ_DOCUMENT where DOC_ID=$id");
          > $row = mysql_fetch_array($result);
          >
          > if($row["MIME_TYPE"] == "application/pdf")
          > $file_name = $row["TITLE"].".pdf";
          > else if($row["MIME_TYPE"] == "application/doc" or $row["MIME_TYPE"]
          > == "application/msword")
          > $file_name = $row["TITLE"].".doc";
          > else if($row["MIME_TYPE"] == "application/mpp")
          > $file_name = $row["TITLE"].".mpp";
          >
          >
          > $file_data = $row['DOC'];
          > $file_type = $row["MIME_TYPE"];
          > $file_size = $row["SIZE"];
          >
          > $handle=fopen($file_name,"wb");
          > fwrite($handle,"$file_data",$file_size);
          > fclose($handle);
          >
          >
          > header("Content-type:$file_type");
          > if(headers_sent())
          > echo ('Some data has already been output to browser, can\'t send
          > file');
          > header("Content-length:$file_size");
          > header('Content-Disposition: attachment;
          filename="'.$file_name.'"');
          > echo $file_data;
          > unlink($file_name);
          >
          > ?>
          >
          > please tell me where i should modify.. i am not able to open donloaded
          > document bcoz of this Read-Error error
          >






          ---------------------------------
          Do you Yahoo!?
          Everyone is raving about the all-new Yahoo! Mail Beta.

          [Non-text portions of this message have been removed]
        • Vinu Thomas
          The following code works for me. To save the file: 0) { $fileName =
          Message 4 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.