Solved

Problems writing binary files to Sybase from PHP

Posted on 2014-11-13
2
191 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
[X]
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
2 Comments
 
LVL 110

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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