Solved

Problems writing binary files to Sybase from PHP

Posted on 2014-11-13
2
178 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update database from a form 8 35
what is best version of php to use 6 46
Load google maps api into wordpress 5 37
mcrypt_create_iv() is deprecated 4 69
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now