Home > Article > Backend Development > How to convert php files to hexadecimal
How to convert php files to hexadecimal: 1. Create a PHP sample file; 2. Convert the file content to hexadecimal output through the "function fileToHex($file){...}" method That’s it.
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
How to convert php files to hexadecimal?
Example of method of converting files to hexadecimal in php
The code is as follows:
<?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)); ?>
Output:
文件转16进制 efbbbf3130e4b8aae4bfafe58da7e69291e28094e280943235e4b8aae4bbb0e58da7e8b5b7... 16进制转文件 boolean true
Recommended study:《 PHP video tutorial》
The above is the detailed content of How to convert php files to hexadecimal. For more information, please follow other related articles on the PHP Chinese website!