您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP如何实现将文件转二进制输出

2025/5/14 4:35:14发布26次查看
php将文件转二进制的方法:首先设置“header( content-type: image/jpeg);”;然后通过fopen和fread函数打开并读取文件;最后将图片文件以二进制流的形式输出到客户端即可。
推荐:《php教程》
php将文件转换成二进制输出
header( "content-type: image/jpeg");$psize = filesize('1.jpg');$picturedata = fread(fopen('1.jpg', "r"), $psize);echo $picturedata;
就这么几句话,就将图片以二进制流的形式输出到客户端了,和打开一张图片没有任何区别,需要注意的是,发送的header要根据具体情况而定,不一定都是image/jpeg。jpg的是它,但png的就是image/png.不同的图片输出不同的头部。
用途:
oss默认支持上传文件流,但input表单默认是返回一个文件:
/** * 支持文件类型上传到oss */ public static function uploadfile($filename, $ext = 'jpg', $type = enum_oss_file_type::img) { $content = static::file2content($filename); return static::upload($content, $ext, $type); } public static function file2content($filename) { return fread(fopen($filename, 'r'), filesize($filename)); }
以上就是php如何实现将文件转二进制输出的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product