views:

61

answers:

1

I have a binary that I want to convert to standard MIME-compliant base64 string. I thought I could use the php://filter method to filter the stream, but I'm getting hung up on how to set the "line-length" parameter. There are other methods that would achieve similar results, but I'm trying to figure out if:

  1. If it's even possible/allowed to set parameters for stream filters using the URI-style method.

  2. How it is done, if it is possible.

Here are the pieces to the puzzle I've got so far:

Obviously, I could stop trying to be fancy and simply go with:

$binary = "path/to/binary.file";
$mime = file_get_contents($binary);
$mime = base64_encode($mime);
$mime = chunk_split($mime);

Or, I can use the php://filter method without the line-length parameter and split it afterward:

$binary = "path/to/binary.file";
$mime = file_get_contents("php://filter/read=convert.base64-encode/resource=$binary");
$mime = chunk_split($mime);

Or, I can use fopen combined with stream_filter_append and not use the URL-style at all (as documented in the Conversion Filters manual):

$binary = "path/to/binary.file";
$param = array('line-length' => 76, 'line-break-chars' => "\r\n");
$fp = fopen($binary, 'r');
stream_filter_append($fp, 'convert.base64-encode',null, $param);
$mime = fread($fp, filesize($binary));
fclose($fp);

So all of the above give the same result, but none of them are what I was hoping for, which was to add the line-length parameter in the same URL for the stream. Something maybe like:

$mime = file_get_contents("php://filter/read=convert.base64-encode:line-length=76/resource=$binary");

I imagine the issue is that the parameter has to be passed as an array when stream_filter_append is used.

So does anyone know if what I want can be done?

+1  A: 
  1. If it's even possible/allowed to set parameters for stream filters using the URI-style method.

Unfortunately, currently, it is not possible1 to provide anything other than the filter name (in your case convert.base64-encode) for those php://filter URLs.

I would go with your stream_filter_append approach, substituting stream_get_contents for fread(...).

  1. See the current source which explicitly uses NULL for the filterparams parameter to php_stream_filter_create. It wouldn't2 be too much work to implement reading of arguments of the style you suggested.
  2. I am not a C programmer.
salathe
Wow. Just what I needed to know. I am also NOT a C programmer, but in my case I wouldn't have even known what to look for or read it, so you're closer than I am. Thank!
Anthony
You're welcome. :)
salathe