English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Preface
This article introduces how to use PHP to implement file and16Binary conversion, for example, you can convert files to16Saved to the database in binary format, and you can also convert16Binary data converted to file and saved.
The code is as follows:
<?php /** * PHP file and16Binary conversion * Date: 2017-01-14 * Author: fdipzone * Ver: 1.0 * * Func * fileToHex File conversion16Binary * hexToFile 16Binary to file */ /** * Convert file content to16Binary output * @param String $file File path * @return String */ function fileToHex($file){ if(file_exists($file)){ $data = file_get_contents($file); return bin2hex($data); } return ''; } /** * Convert16Convert binary content to file * @param String $hexstr 16Binary content * @param String $file The path of the saved file */ function hexToFile($hexstr, $file){ if($hexstr){ $data = pack('H*', $hexstr); file_put_contents($file, $data, true); } } // Demonstration $file = 'test.doc'; // File conversion16Binary $hexstr = fileToHex($file); echo 'File conversion16Binary<br>'; echo $hexstr.'<br><br>'; // 16Binary to file conversion $newfile = 'new.doc'; hexToFile($hexstr, $newfile); echo '16Binary to file<br>'; var_dump(file_exists($newfile)); ?>
Output:
File conversion16Binary efbbbf3130e4b8aae4bfafe58da7e69291e28094e280943235e4b8aae4bbb0e58da7e8b5b7... 16Binary to file conversion boolean true
Summary
That's all for this article. I hope the content of this article can bring you some help in learning or work. If you have any questions, you can leave a message for communication. Thank you for your support of the Yelling Tutorial.
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report by email to codebox.com (replace # with @ when sending an email), and provide relevant evidence. Once verified, this site will immediately delete the infringing content.