/**
* [log 记录执行日志]
* @param [type] $str [description]
* @param [type] $count [description]
* @return [type] [description]
*/
public function log($str,$count)
{
$date = date('Y-m-d H:i:s');
$text = '-------------------------------------------------------------------'.PHP_EOL;
if ($count > 0) {
$text .= "|🐉[ {$str},时间:{$date},row:{$count} ]🐉|".PHP_EOL;
}else{
$text .= "|🐍[ {$str},时间:{$date},row:{$count} ]🐍|".PHP_EOL;
}
$dir = RUNTIME_PATH.'Timing';
if (!is_dir($dir)) {
mkdir($dir);
}
$file_name = $dir.'/Timing_'.date('Y_m_d').'.log';
file_put_contents($file_name,$text,FILE_APPEND);
}
观察者模式
<?php
/**
* @Author: Fyang
* @Email: admin@fyang.vip
* @Date: 2020-08-06 18:07:50
* @Last Modified by: 峰扬
* @Last Modified time: 2020-08-06 18:34:24
* @Project_name: test
* Description : 观察者模式
*/
/**
* 登录完成后需要:
* 1、发送短信
* 2、发送邮件
* 3、推送活动
* 4、登录统计次数
*/
/**
* 被观察者抽象类
* 预定义一个准则类(接口) 所有后续完成的业务都需要继承此类(按照此准则工作)
*/
interface Subject{
public function register(Observer $observer);
public function notify();
}
/**
* 被观察者
* 实现被观察者抽象类
*/
class Action implements Subject{
public $_observers = [];
public function register(Observer $observer){
$this->_observers[] = $observer;
}
public function notify(){
foreach ($this->_observers as $observer) {
$observer->watch();
}
}
}
/**
* 观察者接口
* 预定义一个准则类(接口) 所有后续完成的业务都需要继承此类(按照此准则工作)
*/
interface Observer{
//具体实现方法
public function watch();
}
/**
* 观察者
* 1、发送短信
*/
class Sms implements Observer{
public function watch(){
echo "this Sms class".PHP_EOL.PHP_EOL;
}
}
/**
* 观察者
* 2、发送邮件
*/
class Email implements Observer{
public function watch(){
echo "this Email class".PHP_EOL.PHP_EOL;
}
}
/**
* 观察者
* 3、推送活动
*/
class Activity implements Observer{
public function watch(){
echo "this Activity class".PHP_EOL.PHP_EOL;
}
}
/**
* 观察者
* 4、登录统计次数
*/
class Count implements Observer{
public function watch(){
echo "this Count class".PHP_EOL.PHP_EOL;
}
}
$action = new Action;
$action->register(new Sms());
$action->register(new Email());
$action->register(new Activity());
$action->register(new Count());
print_r($action->notify());
将一个数组键/值将作为另一个数组的键/值
将一个数组键/值将作为另一个数组的键/值
array_combine(array_values($row),$row)
Base64转回文件
上代码
$base_str = "data:audio/mp3;base64,//...[这是一条很长的base64格式的字符串]";
if (preg_match('/^(data:\s*audio\/(\w+);base64,)/', $base_str, $result)){
$type = $result[2];//文件类型
$save_file = "./".date('Ymd',time())."/";//保存路径
if(!file_exists($save_file)){
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($save_file, 0700);
}
$save_file = $new_file.time().".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base_str)))){
dump($new_file);//结果
}else{
dump('失败');
}
}
Mysql权限处理
查看用户信息
select `Host`,`user` from user;
mysql> select `Host`,`user` from user;
+-----------+------------+
| Host | user |
+-----------+------------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------------+
4 rows in set (0.00 sec)
创建一个用户
示例:create user '用户名'@'host权限' identified by '密码';
mysql> create user 'test'@'%' identified by 'MV5vVQUu';
Query OK, 0 rows affected (0.00 sec)
授予用户权限
示例:grant 权限类型(select、update、delete可多个,all 表示所有) privileges on 表名.操作权限(*表示所有) to '用户名'@'host权限'
mysql> grant all privileges on test_db.* to 'test'@'%';
Query OK, 0 rows affected (0.00 sec)
查询用户权限
mysql> show grants for test@'%';
+-----------------------------------------------------------------------------------------------------------+
| Grants for test@% |
+-----------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test'@'%' IDENTIFIED BY PASSWORD '*3EB9B6C888B00C67DA138D280D13B8C5129A1DD2' |
| GRANT ALL PRIVILEGES ON `test_db`.* TO 'test'@'%' |
+-----------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
取消用户权限
示例:revoke all on 表名.操作权限 from test@'%'
mysql> revoke all on test_db.* from test@'%';
Query OK, 0 rows affected (0.00 sec)
再次查看用户权限
mysql> show grants for test@'%';
+-----------------------------------------------------------------------------------------------------------+
| Grants for test@% |
+-----------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test'@'%' IDENTIFIED BY PASSWORD '*3EB9B6C888B00C67DA138D280D13B8C5129A1DD2' |
+-----------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改用户密码
//update 需要指定密码和password函数
//推荐方法
ALTER USER test@'%' IDENTIFIED BY '123456';
最后别忘了刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
Linux中配置apache+php-fpm模式
先查找php-fpm.conf ,发现PHP的安装目录下没有该文件
在PHP安装目录下创建php-fpm配置文件,并输入以下内容(存在跳过此步骤)
[global]
pid = /www/server/php/70/var/run/php-fpm.pid
error_log = /www/logs/php/70/php-fpm.log
log_level = notice
[www]
listen = /tmp/php-cgi-70.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.status_path = /phpfpm_70_status
pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 50
request_terminate_timeout = 100
request_slowlog_timeout = 30
slowlog = /www/logs/php/70/slow.log
找到apache的配置文件,找到 php_module 项(代表你的PHP版本号)注释该项
再找到 mod_proxy.so 和 mod_proxy_fcgi.so 以及 mod_rewrite.so项,解除注释,启用模块
在apache配置文件中加载其它配置(比如网站域名配置)
IncludeOptional /www/server/apache/vhost/*.conf
php-fpm模式5.png
创建测试文件
在web站点根目录下创建index.php
创建文件夹
mkdir /www/wwwroot/build
创建文件
vim /www/wwwroot/build/index.php
在文件中输入以下内容
phpinfo();
保存退出#
创建apache的虚拟主机站点配置文件
在IncludeOptional 项指定的目录下创建一个任意文件名.conf的配置,我这里是index.conf
vim /www/server/apache/vhost/index.conf
输入以下内容 (注意内容中的路径是可以自定义的)#
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot "/www/wwwroot/build" #这里指定web的根目录
ServerName 192.168.1.3 #这里是web网站访问的域名
ServerAlias 192.168.1.3 #这里也是web网站访问的域名
errorDocument 404 /404.html
ErrorLog "/www/logs/www/1.error_log" #这里是web网站访问的日志
CustomLog "/www/logs/www/1.access_log" combined #这里是web网站访问的错误日志
#DENY FILES
<Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$>
Order allow,deny
Deny from all
</Files>
#PHP
<FilesMatch \.php$>
SetHandler "proxy:unix:/tmp/php-cgi-70.sock|fcgi://localhost"
</FilesMatch>
#PATH
<Directory "/www/wwwroot/build"> #这里指定web的根目录
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>
</VirtualHost>
修改所有新创建的文件所属权
重启apache
启动php-fpm
/www/server/php/56/sbin/php-fpm
/www/server/php/70/sbin/php-fpm
浏览器输入地址
访问成功
PHPExcel生成自定义表格
上代码
$objPHPExcel = new \PHPExcel();
// 设置表头 固定部分内容
$objPHPExcel->getActiveSheet()->mergeCells('A1:G1');//合并单元格 从A1到G1
$objPHPExcel->getActiveSheet()->setCellValue('A1',$fileName);//单元格内容
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('宋体');//A1单元格字体类型
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(17);//A1单元格字体大学
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//A1单元格字体加粗
// 序号
$objPHPExcel->getActiveSheet()->mergeCells('A2:A3');//合并单元格 从A2到A3
$objPHPExcel->getActiveSheet()->setCellValue('A2','序号');//A2单元格内容
// 姓名
$objPHPExcel->getActiveSheet()->mergeCells('B2:B3');//合并单元格 从B2到B3
$objPHPExcel->getActiveSheet()->setCellValue('B2','姓名');//B2单元格内容
// 早餐
$objPHPExcel->getActiveSheet()->mergeCells('C2:D2');//合并单元格 从C2到D2
$objPHPExcel->getActiveSheet()->setCellValue('C2','早餐(8元/餐)');//C2单元格内容
$objPHPExcel->getActiveSheet()->setCellValue('C3','餐数');//C3单元格内容
$objPHPExcel->getActiveSheet()->setCellValue('D3','单位补助(4.8元/餐)');//D3单元格内容
// 午餐
$objPHPExcel->getActiveSheet()->mergeCells('E2:F2');
$objPHPExcel->getActiveSheet()->setCellValue('E2','午餐(14元/餐)');
$objPHPExcel->getActiveSheet()->setCellValue('E3','餐数');
$objPHPExcel->getActiveSheet()->setCellValue('F3','单位补助(8.4元/餐)');
//备注
$objPHPExcel->getActiveSheet()->mergeCells('G2:G3');
$objPHPExcel->getActiveSheet()->setCellValue('G2','备注');
//固定列
$start_row = 4;
foreach ($data['list'] as $key => $val) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$start_row,$key + 1);//No.
$objPHPExcel->getActiveSheet()->setCellValue('B'.$start_row,$val['name']);// 姓名
$objPHPExcel->getActiveSheet()->setCellValue('C'.$start_row,$val['breakfast']); // 早餐数
$objPHPExcel->getActiveSheet()->setCellValue('D'.$start_row,$val['breakfast_sum']); // 补助金额
$objPHPExcel->getActiveSheet()->getStyle('D'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$start_row,$val['lunch']); // 晚餐数
$objPHPExcel->getActiveSheet()->setCellValue('F'.$start_row,$val['lunch_sum']); // 补助金额
$objPHPExcel->getActiveSheet()->getStyle('F'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$start_row ++;
}
// $start_row++;
$objPHPExcel->getActiveSheet()->mergeCells('A'.$start_row.':B'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$start_row,'合计');
$objPHPExcel->getActiveSheet()->setCellValue('C'.$start_row, '=SUM(C4:C'.($start_row-1).')');
$objPHPExcel->getActiveSheet()->setCellValue('D'.$start_row, '=SUM(D4:D'.($start_row-1).')');
$objPHPExcel->getActiveSheet()->setCellValue('E'.$start_row, '=SUM(E4:E'.($start_row-1).')');
$objPHPExcel->getActiveSheet()->setCellValue('F'.$start_row, '=SUM(F4:F'.($start_row-1).')');
$objPHPExcel->getActiveSheet()->getStyle('D'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$objPHPExcel->getActiveSheet()->getStyle('F'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
//列+1
$start_row++;
$objPHPExcel->getActiveSheet()->mergeCells('A'.$start_row.':B'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$start_row,'总计');
$objPHPExcel->getActiveSheet()->mergeCells('C'.$start_row.':F'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$start_row, '=SUM(D'.($start_row-1).',F'.($start_row-1).')');
$objPHPExcel->getActiveSheet()->getStyle('C'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$start_row++;
$objPHPExcel->getActiveSheet()->mergeCells('A'.$start_row.':B'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$start_row,'税额(8.5%)');
$objPHPExcel->getActiveSheet()->mergeCells('C'.$start_row.':F'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$start_row, '=C'.($start_row-1).'*0.085');
$objPHPExcel->getActiveSheet()->getStyle('C'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$start_row++;
$objPHPExcel->getActiveSheet()->mergeCells('A'.$start_row.':B'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$start_row,'税后总计');
$objPHPExcel->getActiveSheet()->mergeCells('C'.$start_row.':F'.$start_row);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$start_row, '=SUM(C'.($start_row-2).':C'.($start_row-1).')');
$objPHPExcel->getActiveSheet()->getStyle('C'.$start_row)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
// 居中和水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1:G'.$start_row)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1:G'.$start_row)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 设置边框
$objPHPExcel->getActiveSheet()->getStyle('A2:G'.$start_row)->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
// 落款
$start_row++;
$objPHPExcel->getActiveSheet()->setCellValue('A'.$start_row,'制表人:');
$objPHPExcel->getActiveSheet()->setCellValue('D'.$start_row,'审核人:');
$objPHPExcel->getActiveSheet()->getStyle('A2:G'.$start_row)->getFont()->setName('宋体');
$objPHPExcel->getActiveSheet()->getStyle('A2:G'.$start_row)->getFont()->setSize(12);
// 设置宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(8);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(21);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(21);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$fileName = iconv("utf-8", "gb2312", $fileName.'.xls');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
//创建
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); //文件通过浏览器下载
exit;
Apache禁止域名和未设置虚拟主机的域名访问
LAMP禁止域名和未添加虚拟主机的域名访问,这里只记录一种,访问未设置虚拟主机的域名或IP时跳到指定的页面中
<VirtualHost *:80>
DocumentRoot "指定目录"
ServerName ip地址
ServerAlias *
<Location 指定目录>
Order Allow,Deny
Allow from all
</Location>
</VirtualHost>
有的朋友说该设置需要放在虚拟域名列表的前面,但我此处是先载入虚拟域名的配置,然后才是此设置,具体情况还有待测试