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:
If it's even possible/allowed to set parameters for stream filters using the URI-style method.
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)开发者_如何学JAVA);
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?
- 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(...)
.
- See the current source which explicitly uses
NULL
for thefilterparams
parameter tophp_stream_filter_create
. It wouldn't2 be too much work to implement reading of arguments of the style you suggested. - I am not a C programmer.
精彩评论