Solved

Problems writing binary files to Sybase from PHP

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
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.

759 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

20 Experts available now in Live!

Get 1:1 Help Now