How to convert php files to hexadecimal

藏色散人
Release: 2023-03-14 10:56:01
Original
2560 people have browsed it

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.

How to convert php files to hexadecimal

#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 &#39;&#39;;
}
 
/**
 * 将16进制内容转为文件
 * @param String $hexstr 16进制内容
 * @param String $file 保存的文件路径
 */
function hexToFile($hexstr, $file){
 if($hexstr){
 $data = pack(&#39;H*&#39;, $hexstr);
 file_put_contents($file, $data, true);
 }
}
 
// 演示
$file = &#39;test.doc&#39;;
 
// 文件转16进制
$hexstr = fileToHex($file);
echo &#39;文件转16进制<br>&#39;;
echo $hexstr.&#39;<br><br>&#39;;
 
// 16进制转文件
$newfile = &#39;new.doc&#39;;
hexToFile($hexstr, $newfile);
 
echo &#39;16进制转文件<br>&#39;;
var_dump(file_exists($newfile));
 
?>
Copy after login

Output:

文件转16进制
efbbbf3130e4b8aae4bfafe58da7e69291e28094e280943235e4b8aae4bbb0e58da7e8b5b7...
 
16进制转文件
boolean true
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template