php文件转十六进制的方法:1、创建一个php示例文件;2、通过“function filetohex($file){...}”方法将文件内容转为16进制输出即可。
本文操作环境:windows7系统,php7.4版,dell g3电脑。
php 文件怎么转十六进制?
php实现文件与16进制相互转换的方法示例
代码如下:
<?php/** * php 文件与16进制相互转换 * date: 2017-01-14 * author: fdipzone * ver: 1.0 * * func * filetohex 文件转16进制 * hextofile 16进制转为文件 */ /** * 将文件内容转为16进制输出 * @param string $file 文件路径 * @return string */function filetohex($file){ if(file_exists($file)){ $data = file_get_contents($file); return bin2hex($data); } return '';} /** * 将16进制内容转为文件 * @param string $hexstr 16进制内容 * @param string $file 保存的文件路径 */function hextofile($hexstr, $file){ if($hexstr){ $data = pack('h*', $hexstr); file_put_contents($file, $data, true); }} // 演示$file = 'test.doc'; // 文件转16进制$hexstr = filetohex($file);echo '文件转16进制<br>';echo $hexstr.'<br><br>'; // 16进制转文件$newfile = 'new.doc';hextofile($hexstr, $newfile); echo '16进制转文件<br>';var_dump(file_exists($newfile)); ?>
输出:
文件转16进制efbbbf3130e4b8aae4bfafe58da7e69291e28094e280943235e4b8aae4bbb0e58da7e8b5b7... 16进制转文件boolean true
推荐学习:《php视频教程》
以上就是php 文件怎么转十六进制的详细内容。