Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problems writing binary files to Sybase from PHP

Posted on 2014-11-13
2
188 Views
Last Modified: 2014-12-05
Hi there

We are trying to write PDF, Word, JPG and other binary files to a Sybase SQL Anywhere database using PHP.

We are connecting with PHP using PHP_DBLIB through PDO and are using the code below to read and a file and write into a column:

<?php
// Read file
$filename = “/path/to/file/pdf.pdf";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));

// Attempt to write file
$query = $db->prepare('INSERT INTO table_name (id, document_blob) VALUES (1, ?)’);
$query->bindParam(1, $contents, \PDO::PARAM_LOB);
$query->execute();
?>

The whole query just fails with a generic error message, however we are able to use the above code to write to a MySQL database.

Any thoughts / inspiration gratefully received.

Cheers
Ed
0
Comment
Question by:TedMorey
2 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40444057
There are certain characters that are not "binary safe" when you're writing them into a database.  You might want to consider using base64_encode() before writing and base64_decode() after reading.  The database column widths may need to be expanded since base64_encode() expands the strings by a large amount.

You may want to test the value of $contents. Is it what you expect?  The script does not seem to do anything except assume it's a usable value, and just use it without any understanding of the contents of that variable.

You may want to consider storing not the "blob" data but instead storing only the URL of the PDF.  It's usually wise to store the file path in the database and store the file contents in the server file system.  Reasons include performance and ability to backup the database.
0
 

Author Comment

by:TedMorey
ID: 40445809
Hi Ray

Thanks for getting back to me - it's much appreciated.

Unfortunately the situation does not allow us to try the things you suggest. We are building a web front-end to a legacy application and as such the encoding of documents / storing them as BLOBs are pre-determined for us.

we are still scratching our heads at this end about how to resolve this.

Best wishes
Ed
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question