Pimin's Blog - PHP
type: channel
link: https://pimin.work/index.php/tag/PHP/
items: Redis 键(key) PHP用法
items: php下载文件到指定目录，并打包
items: PHP短开标签
items: PHPExcel快速生成字母表

Redis 键(key) PHP用法
type: item
date: 2022-12-13T18:33:00+08:00
description: Redis keys 命令下表给出了与 Redis 键相关的基本命令：命令描述DEL key该命令用于在 key 存在是删除 keyDUMP key序列化给定 key ，并返回被序列化的值EXISTS key检查给定 key 是否存在EXPIRE key seconds为给定 key 设置过期时间EXPIREAT key timestampEXPIREAT 的作用和 EXPIRE 类似，都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)PEXPIRE key milliseconds设置 key 的过期时间亿以毫秒计PEXPIREAT key milliseconds-timestamp设置 key 过期时间的时间戳(unix timestamp) 以毫秒计KEYS pattern查找所有符合给定模式( pattern)的 keyMOVE key db将当前数据库的 key 移动到给定的数据库 db 当中PERSIST key移除 key 的过期时间，key 将持久保持PTTL key以毫秒为单位返回 key 的剩余的过期时间TTL key以秒为单位，返回给定 key 的剩余生存时间(TTL, time to live)RANDOMKEY从当前数据库中随机返回一个 keyRENAME key newkey修改 key 的名称RENAMENX key newkey仅当 newkey 不存在时，将 key 改名为 newkeyTYPE key返回 key 所储存的值的类型{collapse}{collapse-item label="业务代码（PHP）" open}class RedisKeyDemo{ public $redis; // redis实例 public $type_list = ['Not Found', 'String', 'Set', 'List', 'Zset', 'Hash', 'Stream']; // 数据类型 public function __construct(){ $this->redis = new Redis(); } /** * 链接 Redis * @param string $host * @param int $port * @return bool */ public function connect(string $host = '127.0.0.1', int $port = 6379): bool{ return $this->redis->connect($host, $port); } /** * 打印到html * @param $str */ public function printToHtml(string $str = ''): void{ echo $str." "; } /** * 打印所有keys */ public function printAllKeys(): void{ $this->keys(); } /** * 判断键是否存在 * @param string $key * @return bool|int */ public function isExists(string $key = ''): void{ $this->redis->exists($key) ? $this->printToHtml("$key is exists") : $this->printToHtml("$key is not exists"); } /** * 返回键类型 * @param string $type */ public function keyType(string $key = ''): void{ $this->printToHtml($this->type_list[$this->redis->type($key)]); } /** * 删除指定键 * 原本方法可以 key1,key2···, 接受string或array * @param string $key */ public function delKey(string $key = ''): void{ $this->printToHtml($this->redis->del($key) ? "'$key'删除成功" : "'$key'删除失败"); } /** * 为指定键设置过期时间 * @param string $key * @param int $ttl 过期时间 * @param string $type second 使用秒， timestamp 使用时间戳， milliseconds …

php下载文件到指定目录，并打包
type: item
date: 2022-10-23T18:00:12+08:00
description: 前几天有这么个需求，以后可能也用的上，先保存下来。下载文件到指定目录/** * 下载文件到指定目录 * @param $file_url 请求下载的链接 * @param $path 保存位置 * @param string $save_name 文件名，留空使用下载来的文件名 * @return string 返回下载的文件路径 */ public function downloadFile($file_url, $path, $save_name = ''){ $basePath = DIR_ROOT."upfile/"; if($path){ $basePath .= $path."/"; } $dir_path = $basePath; if (!is_dir($dir_path)) { mkdir($dir_path, 0777, true); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $file_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); $file = curl_exec($ch); curl_close($ch); //传入保存文件的名称 $filename = $save_name ?: pathinfo($file_url, PATHINFO_BASENAME); $resource = fopen($dir_path. '/'. $filename, 'a'); fwrite($resource, $file); fclose($resource); return $basePath . '/' . $filename; }打包文件/** * 打包文件 * @param $path 需要打包的路径 * @param $zip ZipArchive对象 * @param string $child_path 递归子文件夹用的 */ public function addFileToZip($path, &$zip, $child_path = ''){ $handler = opendir($path); //打开当前文件夹由$path指定。 while(($filename=readdir($handler)) !== false){ if($filename != "." && $filename != ".."){ //文件夹文件名字为'.'和‘..'，不要对他们进行操作 if(is_dir($path."/".$filename)){ // 如果读取的某个对象是文件夹，则递归 $zip->addEmptyDir($filename); // 这里注意：php只需传递一个文件夹名称路径即可 $this->addFileToZip($path . "/" . $filename, $zip, $filename); }else{ //将文件加入zip对象 if (!empty($child_path)){ $zip->addFile($path."/".$filename, $child_path."/".$filename); } else{ $zip->addFile($path."/".$filename, $filename); } } } } @closedir($path); }{alert type="warning"}注意调用上面的打包方法时，需要先实例化ZipArchive对象，上示例:{/alert}// 下载到服务器后将文件夹打包为zip $outName = $this->datasheetBase->FormatJobCode($job_code_id).'.zip'; // 压缩包名称 $zipPath = DIR_ROOT."upfile/output/$outName"; // 导出的路径 if (file_exists($zipPath)) @unlink($zipPath); // 清理旧文件 $zip = new ZipArchive(); $data_file = DIR_ROOT."upfile/zip/".$this->customer->getID(); // 需要打包的路径 if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE){ if (!is_dir($data_file)) mkdir($data_file); $this->datasheetBase->addFileToZip($data…

PHP短开标签
type: item
date: 2022-05-27T11:25:00+08:00
description: 从PHP5.4开始支持短开标签假设已有变量 $str = 'Hello';使用正常方式输出:<?php echo $str; ?>使用短开标签方式输出<?= $str; ?>短开标签<?= 是echo的快捷用法

PHPExcel快速生成字母表
type: item
date: 2022-05-26T17:47:00+08:00
description: 用于在字段很多的情况下自动生成列标识, 代码如下$count = 26 * 27; $l = 'A'; $arr = []; while ($count--){ $arr[] = $l; $l++; }这样$arr里面就是excel的列字母表了, 具体数据如下, 怎么用看业务需求吧:["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ","AK","AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","A Z","BA","BB","BC","BD","BE","BF","BG","BH","BI","BJ","BK","BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX","BY","BZ","CA","CB","CC","CD","CE","CF","CG","CH","CI","CJ","CK","CL","CM","CN","CO","CP","CQ","CR","CS","CT" ,"CU","CV","CW","CX","CY","CZ","DA","DB","DC","DD","DE","DF","DG","DH","DI","DJ","DK","DL","DM","DN","DO","DP","DQ","DR","DS","DT","DU","DV","DW","DX","DY","DZ","EA","EB","EC","ED","EE","EF","EG","EH","EI","EJ","EK","EL","EM","EN"," EO","EP","EQ","ER","ES","ET","EU","EV","EW","EX","EY","EZ","FA","FB","FC","FD","FE","FF","FG","FH","FI","FJ","FK","FL","FM","FN","FO","FP","FQ","FR","FS","FT","FU","FV","FW","FX","FY","FZ","GA","GB","GC","GD","GE","GF","GG","GH","GI ","GJ","GK","GL","GM","GN","GO","GP","GQ","GR","GS","GT","GU","GV","GW","GX","GY","GZ","HA","HB","HC","HD","HE","HF","HG","HH","HI","HJ","HK","HL","HM","HN","HO","HP","HQ","HR","HS","HT","HU","HV","HW","HX","HY","HZ","IA","IB","IC", "ID","IE","IF","IG","IH","II","IJ","IK","IL","IM","IN","IO","IP","IQ","IR","IS","IT","IU","IV","IW","IX","IY","IZ","JA","JB","JC","JD","JE","JF","JG","JH","JI","JJ","JK","JL","JM","JN","JO","JP","JQ","JR","JS","JT","JU","JV","JW","J X","JY","JZ","KA","KB","KC","KD","KE","KF","KG","KH","KI","KJ","KK","KL","KM","KN","KO","KP","KQ","KR","KS","KT","KU","KV","KW","KX","KY","KZ","LA","LB","LC","LD","LE","LF","LG","LH","LI","LJ","LK","LL","LM","LN","LO","LP","LQ","LR" ,"LS","LT","LU","LV","LW","LX","LY","LZ","MA","MB","MC","MD","ME","MF","MG","MH","MI","MJ","MK","ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NB","NC","ND","NE","NF","NG","NH","NI","NJ","NK","…