?
Solved

Problems writing binary files to Sybase from PHP

Posted on 2014-11-13
2
Medium Priority
?
208 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.
Suggested Courses

771 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