1、Redis 队列实现
①redis函数rpush,lpop
<?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $password = '123456'; $redis->auth($password); $arr = array('h','e','l','l','o','w','o','r','l','d'); foreach($arr as $k=>$v){ $redis->rpush("mylist",$v); }
$redis = new Redis(); $redis->connect('127.0.0.1',6379); $password = '123456'; $redis->auth($password); //list类型出队操作 $value = $redis->lpop('mylist'); if($value){ echo "出队的值".$value; }else{ echo "出队完成"; }
?>
关于时间
获取上个月的最后一天
function get_last_month_last_day($date = '')
{
if ($date != '') {
$time = strtotime($date);
} else {
$time = time();
}
$day = date('j', $time);//获取该日期是当前月的第几天
return date('Y-m-d', strtotime("-{$day} days", $time));
}
关于无限分类
function tree($arr, $pid = 0, $level = 0)
{
static $list = array();
foreach ($arr as $v) {
//如果是顶级分类,则将其存到$list中,并以此节点为根节点,遍历其子节点
if ($v['parent_id'] == $pid) {
$v['level'] = $level;
$list[] = $v;
tree($arr, $v['cat_id'], $level + 1);
}
}
return $list;
}
关于文件
遍历一个文件夹下的所有文件和子文件夹
function my_scandir($dir)
{
$files = array();
if (is_dir($dir)) {
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) != false) {
if ($file != "." && $file != "..") {
if (is_dir($dir . "/" . $file)) {
$files[$file] = my_scandir($dir . "/" . $file);
} else {
$files[] = $dir . "/" . $file;
}
}
}
closedir($handle);
return $files;
}
}
}
读取文件加锁和解锁
$fp = fopen("lock.txt","w+");
if (flock($fp,LOCK_EX)) {
//获得写锁,写数据
fwrite($fp, "write something");
// 解除锁定
flock($fp, LOCK_UN);
} else {
echo "file is locking...";
}
fclose($fp);
获取文件扩展名
//plan A
function get_ext(string $url){
//$url = 'http://www.sina.com.cn/abc/de/fg.html?id=1&ajksfg&aakzsdfj';
$a = parse_url($url); //Array ( [scheme] => http [host] => www.sina.com.cn [path] => /abc/de/fg.html [query] => id=1&ajksfg&aakzsdfj )
$file = basename($a['path']); //fg.html
$b = explode('.',$file);
return array_pop($b);
}
//plan B
function get_ext(string $url){
//$url = 'http://www.sina.com.cn/abc/de/fg.html?id=1&ajksfg&aakzsdfj';
$a = basename($url); //fg.html?id=1&ajksfg&aakzsdfj
$b = explode('?',$a);; //Array ( [0] => fg.html [1] => id=1&ajksfg&aakzsdfj )
$ext = explode('.',$b[0]);
return array_pop($ext);
}
关于框架使用笔记
thinkphp6 文件上传
$fileName = Filesystem::putFile(‘video’, $file, ‘uniqid’);
uniqid:没有日期文件夹,存在runtime文件夹里的
$fileName = Filesystem::disk('public')->putFile('file', $file);//disk('文件夹)
$result = validate(['file' => ['fileExt:xlsx']])->check(['file' => $file]);//验证文件