Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Odbc Insert pdf into Memo from PHP script
Thu, Dec 17 2015 11:16 PMPermanent Link

Barry Schofield

I have a client where we need to insert documents of tiff or pdf format directly into a memo field of existing software.
I was told by the company that I needed odbc v 4.26 r 3, which I purchased, and still not resolving the problem.
I am not able to insert any image or document types into the memo field,

here is the code snippet, I am leaving in all attempts using the $content variable.



if (isset ($_POST['submit'])) { // Handle form.
   
   // Loop $_FILES to execute all files
   for($i=0; $i < count($_FILES['files']['tmp_name']);$i++)
       {
   
// create temp name made from file name
            $fileName = $_FILES['files']['name'] [$i];
            $tmpName  = $_FILES['files']['tmp_name'] [$i];
            
            $fileSize = $_FILES['files']['size'] [$i];
            $fileType = $_FILES['files']['type'] [$i];
            $fp      = fopen($tmpName, 'r');
            $content = addslashes(file_get_contents ($_FILES['files']['tmp_name'] [$i]));
            //$content = fread($fp, filesize($tmpName));
            //$content = readfile($tmpName);
              // $content = addslashes($content);
            fclose($fp);
            


       $dtmp=  date("Ymd_His");
      
      
   $tempfilename = $thepatient."_".$doc_cat."_".$dtmp."_".$_FILES['files']['name'] [$i];
   // Try to move the uploaded file.
      $fullfile= $_docdir.$tempfilename;
   
   
      if (move_uploaded_file ($_FILES["files"]["tmp_name"] [$i], $fullfile)) {
              
   
      print "<font color=red>&#187; $fullfile has been uploaded. DELETE TEMP FILE! </font>";
            // now insert into data 47
         
            $system_c = date("U");
         
         
            $patient = $_POST['THEPATIENT'];
            $employee = $_POST['EMPLOYEE'];
                               
            $sql47 = "INSERT INTO dcd3 ( [date],[type], [chart number],document, [name]) VALUES ('$docdate',$doc_cat,'$thepatient' , '$content' ,' $desc')";
             @odbc_exec($conn,$sql47) or die('error inserting to document for DCDocument');



Thank you
Barry

Fri, Dec 18 2015 9:16 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

<< I have a client where we need to insert documents of tiff or pdf format directly into a memo field of existing software.  I was told by the company that I needed odbc v 4.26 r 3, which I purchased, and still not resolving the problem. I am not able to insert any image or document types into the memo field, >>

Are you actually using version 4.26 of the ODBC driver, or the latest 4.42 ?

Also, you're not seeing any error messages in the Apache PHP logs (I assume that you're using Apache) ?

In order to investigate this further, I'm going to need the target table that you're trying to insert into.  Please email the table files (.dat, .idx, and .blb) to me as a .zip.

Thanks,

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Dec 21 2015 9:19 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

Okay, I checked things out and the issue is with how you're specifying the BLOB data.  You can't just insert BLOB data (binary) into an INSERT SQL statement as a literal string expression.  Instead, you have to use a parameterized INSERT statement.  However, the old-style ODBC access does not allow for binding parameters, so you'll need to use the ODBC PDO driver.  The code would look like this:

<?php

$conn = new PDO("odbc:Temp", "", "");

if ($conn) {

   $fileName = "c:\\temp\\webimage.png";
   $fp = fopen($fileName, "r");
   $content = fread($fp, filesize($fileName));
   fclose($fp);

   $stmt = $conn->prepare("INSERT INTO dcd3 (Document) VALUES (?)");
   $stmt->bindValue(1, $content, PDO::PARAM_LOB);
   $stmt->execute();

} else{
   die("Connection could not be established.");
}
?>

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Dec 22 2015 6:29 PMPermanent Link

Barry Schofield

Tim, I have converted to PDO, and if I put static values in instead of parameters of ? or of named parameters,and it works on all types of data except for the BLOB. if I use parameters it freezes, and I have yet to get a functional import of a BLOB. is this a result of the version of odbc?  I'm using 4.26 r3
I've tried your code exactly, only changing the file name and no functionality.

Thank you very much for your assistance
Barry Schofield






Tim Young [Elevate Software] wrote:

Barry,

Okay, I checked things out and the issue is with how you're specifying the BLOB data.  You can't just insert BLOB data (binary) into an INSERT SQL statement as a literal string expression.  Instead, you have to use a parameterized INSERT statement.  However, the old-style ODBC access does not allow for binding parameters, so you'll need to use the ODBC PDO driver.  The code would look like this:

<?php

$conn = new PDO("odbc:Temp", "", "");

if ($conn) {

   $fileName = "c:\\temp\\webimage.png";
   $fp = fopen($fileName, "r");
   $content = fread($fp, filesize($fileName));
   fclose($fp);

   $stmt = $conn->prepare("INSERT INTO dcd3 (Document) VALUES (?)");
   $stmt->bindValue(1, $content, PDO::PARAM_LOB);
   $stmt->execute();

} else{
   die("Connection could not be established.");
}
?>

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Dec 29 2015 12:42 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

Sorry for the delay in responding.  I missed this post right before Christmas.

<< Tim, I have converted to PDO, and if I put static values in instead of parameters of ? or of named parameters,and it works on all types of data except for the BLOB. if I use parameters it freezes, and I have yet to get a functional import of a BLOB. is this a result of the version of odbc?  I'm using 4.26 r3
I've tried your code exactly, only changing the file name and no functionality. >>

It could very well be a bug in the older version of the driver.  You should try the latest 4.42 version of the ODBC Driver, which worked fine here for me.  However, *do not* do so on a production database that is being updated using any other versions of DBISAM prior to 4.28.  Instead, make a copy of the database and test against the isolated copy.  There was change in how BLOB blocks are recycled in 4.28 that forbids mixing < 4.28 with >= 4.28:

"There is a new design for the BLOB reading/writing that is not compatible with 4.27 or earlier. Therefore, it is extremely important that you do not mix DBISAM 4.28 applications with DBISAM 4.27 or earlier applications. Mixing the two versions can cause improper truncation of BLOB data, and that includes using a 4.27 application on any data that has been updated by DBISAM 4.28, even if it is not concurrently. The issue is that 4.27 and earlier versions recycle BLOB blocks on a per-block basis, whereas 4.28 and higher versions recycle BLOB blocks on a per-row basis."

Tim Young
Elevate Software
www.elevatesoft.com
Image