PHP 笔记

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]);//验证文件       在这里插入图片描述

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值