根据文件头进制特征识别文件类型,识别为图片进行OCR识别,懂得都懂,禁止用于违法行为。
<?php
// 16进制特征码
$checkMap = [
".jpeg" => hex2bin("FFD8FF"),
".png" => hex2bin("89504E47"),
".gif" => hex2bin("47494638"),
".tif" => hex2bin("49492A00"),
".bmp" => hex2bin("424D")
];
// 传递微信加密文件
$file = './be27b0c8911ffd7294b7febc16a9cb91_t.dat';
if (!file_exists($file)) {
throw new Exception('文件' . $file . '不存在');
}
// 读取文件内容
$content = file_get_contents($file);
if (!$content) {
throw new Exception('未获取到文件内容');
}
// 读取文件前10个字节内容作为特征码
$bytesToRead = 10;
$feature = substr($content, 0, 10);
if (!$feature || strlen($feature) != 10) {
throw new Exception('未获取到文件特征码');
}
// 检查特征
$feature_array = str_split($feature, 1);
$fileRes = checkImage($feature_array);
if (empty($fileRes['0'])) {
throw new Exception('不支持的文件类型');
}
// 生成新的文件
$newFile = 'test' . $fileRes['0'];
// 将文件内容转换
$newChar = '';
for ($i = 0; $i < strlen($content); $i++) {
$newChar .= $content[$i] ^ $fileRes['1'];
}
file_put_contents($newFile, $newChar);
// 检查特征
function checkImage($in_feature)
{
global $checkMap;
foreach ($checkMap as $key => $check_feature) {
$isConform = checkConformRule($check_feature, $in_feature);
if ($isConform) {
return [$key, $isConform];
}
}
return [null, null];
}
// 检查规则
function checkConformRule($check_feature, $in_feature)
{
$initDecodeByte = $check_feature[0] ^ $in_feature[0];
for ($i = 0; $i < strlen($check_feature); $i++) {
$b = $check_feature[$i] ^ $in_feature[$i];
if ($b !== $initDecodeByte) {
return false;
}
}
return $initDecodeByte;
}这里给出的是Demo,事实上正式的网站对HTTP请求头要求完整性非常严格,建议完善请求头. 1.先看验证文件:<?php if($_COOKIE['username']=='a...
PHP的session文件夹默认保存在同一个文件,随着访客的增多,访问的速度会非常慢,例如部分管理系统后台每天登陆1000次,1个月就创建了30000个session文件,一年后是多少?假设这个系统的用户是上万人呢?方法1:每个网站使用自己的session文件夹,代码如下:<?php $pat...
前面的文章对于高并发下单商品导致商品库存为负值的问题请先阅读再阅读本篇文章一定对您有帮助,建议亲手测试较好。加上文件锁后的下单处理代码:【一】.阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行)<?php //连接数据库 $con=mysqli_connect(...
使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...
主要原理是通过PHP创建多个子进程,在子进程中发送进程闹钟信号,然后再监听闹钟信号中继续发送闹钟信号。同时通过父进程设置非阻塞运行。代码如下:<?php /** * 订单任务 */ class Order { &n...
(1)swoole启动的主进程是master进程负责全局管理,然后master进程会再fork一个manager进程。(2)manager进程开始统一管理进程创建回收管理。(3)manager进程根据设置的worker_num和task_worker_num来创建work进程和task进程因此启动s...