php处理CSV文件乱码之mb_detect_encoding

2020年2月29日 PHP

PHP在处理csv在数据导入导出方面,要比.xls高效很多。

实现一上传csv文件导入功能时,遇到上传文件编码不是UTF-8的情况,导致读取的数据乱码。如果csv文件是utf-8文件,可能导致excel不能正常识别,所以就遇到的问题。

搜索后发现mb_detect_encoding可以检测字符串编码,不一定百分百识别,至少能满足我的需求

代码片断:

            $tmp_row = array();
            foreach ($csv_val as $k => $v) {
                $row_value = ltrim($row[$k], '`');
                $encode = mb_detect_encoding($row_value, array("UTF-8", "GB2312", "CP936", "GBK"));
                if( $encode!="UTF-8" ){
                    $tmp_row[$v] = trim(iconv($encode,'UTF-8', $row_value));
                }else{
                    $tmp_row[$v] = trim($row_value);
                }
            }
            $data[] = $tmp_row;

debian9手动安装nodejs和npm

2020年2月27日 nodejs

1. nodejs官网,找linux64版本,

#wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz

2.解压下载后的压缩文件并拷贝到opt

#tar xvf node-v12.16.1-linux-x64.tar.xz

#mv xvf node-v12.16.1-linux-x64/ /opt/

3.添加到全局path里面

在/etc/profile中添加环境变量(在export PATH的上一行添加就可以)

#vi /etc/profile

PATH=$PATH:/opt/node-v12.16.1-linux-x64/bin

使profile立即生效

#source /etc/profile

4.检查node和npm 版本

#node -v

#npm -v

php artisan config:cache使env函数读取环境变量为null

2020年2月8日 未分类

在 Laravel 项目中,如果执行了

php artisan config:cache

命令把配置文件缓存起来后,在 Tinker 中(Tinker 是 Laravel 自带的一个交互式命令行界面),使用

env

函数读取环境变量的值为

null

,只有执行

php artisan config:clear

清除配置缓存后就可以读取了

在 Laravel 中,如果执行

php aritisan config:cache

命令,Laravel 将会把 app/config 目录下的所有配置文件“编译”整合成一个缓存配置文件到  bootstrap/cache/config.php,每个配置文件都可以通过

env

函数读取环境变量,这里是可以读取的。但是一旦有了这个缓存配置文件,在其他地方使用

env

函数是读取不到环境变量的,所以返回

null

.

如果存在缓存配置文件,就不会去设置环境变量了,配置都读缓存配置文件,而不会再读环境变量了。

因此,在配置文件即 app/config 目录下的其他地方,读取配置不要使用

env

函数去读环境变量,这样你一旦执行

 php artisan config:cache

之后,

env

函数就不起作用了。所有要用到的环境变量,在 app/config 目录的配置文件中通过 env 读取,其他地方要用到环境变量的都统一读配置文件而不是使用

env

函数读取。

 

ProxyChains命令行穿墙

2019年11月2日 未分类

ProxyChains介绍

        ProxyChains遵循GNU协议的一款适用于linux系统的网络代理设置工具。强制由任一程序发起的TCP连接请求必须通过诸如TOR 或 SOCKS4, SOCKS5 或HTTP(S) 代理。支持的认证方式包括:SOCKS4/5的用户/密码认证,HTTP的基本认证。允许TCP和DNS通过代理隧道,并且可配置多个代理。

proxychains功能

使用composer的时候会无法更新,proxychains可以让命令通过指定的proxy访问网络。
例如:


wget www.google.com

由于防火墙的原因,直接访问不通。
如果已经有一个代理服务(socks5://127.0.0.1:1080),配置proxychains之后:


proxychains wget www.google.com

可以正常访问了

nginx try_files 导致自动被加9000端口隐藏之port_in_redirect off;

2019年6月13日 nginx

由于需要nginx反向代理负载,跳转目录URL会自动被加上非80端口(如9000,当启用https时9000端口号会导致证书错误),需要隐藏。

通过port_in_redirect off;可实现端口号隐藏,设置重启后需要清除浏览器缓存。try_files $uri $uri/ @rewrite;会生成301永久重定向,部分浏览器会记住而导致设置无效。

upstream upstream.lenmot {
    #ip_hash;
    server 192.168.1.201:9000 weight=5 max_fails=2 fail_timeout=5s;
    server 192.168.1.202:9000 backup;
}

server {
    listen 80;
    server_name *.lenmot.com;

    return      301 https://$host$request_uri;
}

server {
    listen 443;
    server_name *.lenmot.com;

    ssl on;
    ssl_certificate     /etc/nginx/keys/***.crt;
    ssl_certificate_key /etc/nginx/keys/***.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;

    ssl_prefer_server_ciphers on;
    ssl_ciphers "***";
    
    location / {
        proxy_pass  http://upstream.lenmot;
        proxy_redirect off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
#        proxy_buffer_size       32k;
#        proxy_buffers           4 32k;
#        proxy_busy_buffers_size       64k;
#        proxy_temp_file_write_size    64k;
        #proxy_max_temp_file_size 0;
        #proxy_connect_timeout   90;
        #proxy_send_timeout      90;
        #proxy_read_timeout      90;
    }
}

server {
    listen 9000;
    server_name *.lenmot.com;

    root /usr/local/www/lenmot/www;

    index index.php index.html index.htm;

    add_header  Strict-Transport-Security  "max-age=31536000;includeSubDomains";

    port_in_redirect off;
    try_files $uri $uri/ @rewrite;
    location @rewrite {
        rewrite ^/(.*)$ /index.php?s=$1 last;
    }

    # PHP FPM configuration.
    location ~ \.php {
        fastcgi_pass                    unix:/var/run/php5-fpm.sock;
        fastcgi_index                   index.php;
        fastcgi_split_path_info         ^(.+\.php)(.*)$;
        include                         /etc/nginx/fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ /\.ht {
        deny all;
    }
}

git项目迁移

2018年12月29日 git

1.克隆老项目的镜像

git clone –mirror old.git (old.git 为老项目的git地址)

2.进入老项目的目录

cd old.git

3.移除老项目的地址替换成新项目

git remote set-url –push origin  new.git (new.git 为新项目的git地址)

4.将镜像推到远程

git push –mirror  //这一步需要输入新的git的账号和密码