PHP PHP.net news server web interface
From: Date: Mon Oct 31 13:29:04 2016
Subject: Re: Storing binary data in mySQL
References: 1 2 3 4  Groups: php.windows 
On 27.10.2016 at 02:40, Jacob Kruger wrote:

> Next question then - how did you read the file's contents into a
> variable before encoding it?
> 
> Ask since, base64_encode requires a string parameter, and, if I read the
> files in using fopen($s_file_name, "rb"), then it returns a resource,

fopen() isn't supposed to read a file, but it returns a resource which
can be passed to fread(), for instance, to read the contents of the
file.  Probably the simplest solution:

  <?php
  $stream = fopen($file_name, 'rb');
  $contents = stream_get_contents($stream);
  fclose($stream);

After that, $contents is supposed to hold the exact contents of the file.

> whereas if I use file_get_contents, to read the audio clips contents in
> as a string, and then encode them, they still get decoded as a corrupted
> version, most likely due to reading in binary data the wrong way?

I'm not sure if there are any issues with file_get_contents() wrt.
automatic line ending conversion – at least I've never noticed any.  But
you can easily check that for yourself: read the file with
file_get_contents(), and compare the strlen() of the return value with
the file size.

-- 
Christoph M. Becker

« previous php.windows (#31428) next »

Written by Jim Winstead. no rights reserved. (source code)