【问题标题】:How to convert a byte array to file in php?如何在php中将字节数组转换为文件?
【发布时间】:2014-08-15 22:59:46
【问题描述】:

如何将包含图像文件的 zip 文件夹的 byte array 转换为 php 中的 zip folder ?我找不到解决方案,所以请帮助我..

这是我的byte array 和我的代码..

$photoObject ="504b 0304 1400 0000 0000 818a d944 0000
0000 0000 0000 0000 0000 0b00 0000 4e65
7720 666f 6c64 6572 2f50 4b03 0414 0000
0008 006b 9085 44c9 0419 fceb 5001 008c
6c01 002b 0000 004e 6577 2066 6f6c 6465
722f 7363 7265 656e 7368 6f74 202d 2074
7265 6174 6d65 6e74 6f70 7469 6f6e 2e50
4e47 945a 6750 935d 168e 652d 6b41 41e9
45a5 8934 41e9 1054 a423 7c80 80d4 2845";

$hex         = preg_replace('/[\s\W]+/','',$photoObject);
$binary      = pack("H*", $hex);
$photoObject = base64_decode($binary);
$im          = imagecreatefromstring($binary);

$photoName   = preg_replace('/\s+/', '_' , "zipname");

if ($im !== false) {
    $filedb = '../zipper/'.time().$photoName;
    imagepng($im, $filedb);
    imagedestroy($im);
    return $filedb;
}
else {
    return "error";
}

【问题讨论】:

标签: php file bytearray


【解决方案1】:

如果您的字符串是存档的有效表示,php 可以将其转换回常规文件/二进制字符串。

<?php

$bString = hex2bin($hex);

file_put_contents('new_zip_path', $bString);

$zip = new ZipArchive;
$zip->open('new_zip_path', ...);

$image = $zip->getFromName('my.jpg');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2017-03-15
    • 2015-08-09
    相关资源
    最近更新 更多