将二维码插件qrcodes放在 \ThinkPHP\Library\Vendor\
qrcodes目录结构如下:
创建一个控制器:
准备几张有内容的二维码图片:
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ Vendor('qrcodes.QrReader'); //引入 $file = dir_list('./Public/qrcodes'); //二维码图片路径 foreach($file as $vo) { print basename($vo); //获取图片名称 print ' --- '; $qrcode = new \QrReader($vo); //实例化类 $vo 是 图片路径 ./Public/qrcodes/a.jpg print $text = $qrcode->text(); // 二维码内容 print "<br/>"; } $this->display(); } }
读取文件信息的函数: 放在 Application\Common\Common中....
/** * [检查文件或者目录] * @param [type] $path [文件路径] * @param string $exts [类型] * @param array $list [返回数组] * @return [type] * @author [小柯] <[972581428@qq.com]> */ function dir_list($path, $exts = '', $list= array()) { $path = dir_path($path); $files = glob($path.'*'); foreach($files as $v) { $fileext = fileext($v); if (!$exts || preg_match("/\.($exts)/i", $v)) { $list[] = $v; if (is_dir($v)) { $list = dir_list($v, $exts, $list); } } } return $list; } /** * [检查文件名称] * @param [type] $filename [文件名称] * @return [type] * @author [小柯] <[972581428@qq.com]> */ function fileext($filename) { return strtolower(trim(substr(strrchr($filename, '.'), 1, 10))); } /** * [检查目录是否合法] * @param [type] $path [目录路径] * @return [type] * @author [小柯] <[972581428@qq.com]> */ function dir_path($path) { $path = str_replace('\\', '/', $path); if(substr($path, -1) != '/') $path = $path.'/'; return $path; }
读取成功的效果: