多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
![](https://img.kancloud.cn/1e/6c/1e6c3bdb9cb831a6cb2efe72429e3503_403x391.png)在php原生开发中使用的打印数组一般使用print_r()函数来打印出来,之前在thinkphp5中习惯性使用dump或者echo一时改不过来了,以至于搞了大半天才发现是这个问题。 PHP 7 增加了可以为 unserialize() 提供过滤的特性,可以防止非法数据进行代码注入,提供了更安全的反序列化数据。 ## 定义和用法 file\_get\_contents() 把整个文件读入一个字符串中。 该函数是用于把文件的内容读入到一个字符串中的首选方法。如果服务器操作系统支持,还会使用内存映射技术来增强性能。 ## 语法 file\_get\_contents(path,include\_path,context,start,max\_length) | 参数 | 描述 | | :-- | :-- | | path | 必需。规定要读取的文件。 | | include\_path | 可选。如果您还想在 include\_path(在 php.ini 中)中搜索文件的话,请设置该参数为 '1'。 | | context | 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。若使用 NULL,则忽略。 | | start | 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 中新增的。 | | max\_length | 可选。规定读取的字节数。该参数是 PHP 5.1 中新增的。 | ## 定义和用法 file\_exists() 函数检查文件或目录是否存在。 如果指定的文件或目录存在则返回 TRUE,否则返回 FALSE。 ## 语法 file\_exists(path) | 参数 | 描述 | | :-- | :-- | | path | 必需。规定要检查的路径。 | 案例实例: ~~~ private function path($goods_id){ return '../heutoi/img/'.$goods_id.'/bongan.txt'; } public function get($goods_id){ $p = $this->path($goods_id); if(file_exists($p)){ $f = unserialize(file_get_contents($p)); return $f; }else{ return array(); } } ~~~ php获取数组某个数值方法:array[序列值][名字] 示例:~~~ $array[1]['id'] 记录php对数组值分类的代码案例: ![](https://img.kancloud.cn/1e/6c/1e6c3bdb9cb831a6cb2efe72429e3503_403x391.png) 这里记录一下$_FILES的意思: $\_FILES\[字段名\]\[name\]——保存的文件在上传者机器上的文件名, $\_FILES\[字段名\]\[tmp\_name\]——保存的是文件上传到服务器临时文件夹之后的文件名 $\_files主要用在当需要上传二进制文件的地方,录入上传一个abc.mp3文件,则[服务器端](https://www.baidu.com/s?wd=%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF&tn=SE_PcZhidaonwhc_ngpagmjz&rsv_dl=gh_pc_zhidao)需要获得该文件的相关信息,则通过变量$\_files来取得。 $\_FILES\['userfile'\]\['name'\] 客户端机器文件的原名称。 $\_FILES\['userfile'\]\['type'\] 文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。 $\_FILES\['userfile'\]\['size'\] 已上传文件的大小,单位为字节。 $\_FILES\['userfile'\]\['tmp\_name'\] 文件被上传后在服务端储存的临时文件名。 $\_FILES\['userfile'\]\['error'\] 和该文件上传相关的[错误代码](https://www.baidu.com/s?wd=%E9%94%99%E8%AF%AF%E4%BB%A3%E7%A0%81&tn=SE_PcZhidaonwhc_ngpagmjz&rsv_dl=gh_pc_zhidao)。\['error'\] 是在 PHP 4.2.0 版本中增加的。 注: 在 PHP 4.1.0 版本以前该数组的名称为 $HTTP\_POST\_FILES,它并不像 $\_FILES 一样是自动全局变量。PHP 3 不支持 $HTTP\_POST\_FILES 数组。 如果表单中没有选择上传的文件,则 PHP 变量 $\_FILES\['userfile'\]\['size'\] 的值将为 0,$\_FILES\['userfile'\]\['tmp\_name'\] 将为 none。 这里记录一个php压缩图片上传的方法吧: 使用多个input 框上传,这个种方法比较普通,就略过了 使用一个input框上传多图 先看代码: <form id="uploadForm" method="post" enctype="multipart/form-data" action=""> <textarea id="content" class="form-control" rows="4" name="content"> </textarea> <input id="myfile_input" type="file" name="myfile[]" multiple="multiple" accept="image/*" /> </form> 1 2 3 4 5 form 表单的重点在 enctype=”multipart/form-data” input 的重点在于 multiple=”multiple” accept=”image/*” 参数具体介绍自己百度吧. ps: 这样设置后,input框弹出文件选择时,可以多选文件 php 后台接收处理 普通参数: from 表单的其他参数正常获取 文件对象:使用$_FILES 对象获取 if (isset($_FILES['myfile'])){ $total = count($_FILES['myfile']['name']); // Loop through each file for ($i = 0; $i < $total; $i++) { //Get the temp file path $tmpFileName = $_FILES['myfile']['name'][$i]; $tmp = explode(".", $tmpFileName); $code = $customer_id . rand(1000, 9999); $tempFileName = $code . '.' . $tmp[1]; $small_image = $code . '_small' . '.' . $tmp[1]; $url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $tempFileName; $small_url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $small_image; //Make sure we have a file path if ($tmpFileName != "") { // 上传到本地方式 if (file_exists($url))//当文件存在 { $tempFileName = $code . '-1' . '.' . $tmp[1]; $small_image = $code . '-1_small' . '.' . $tmp[1]; $url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $tempFileName; $small_url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $small_image; } move_uploaded_file($_FILES["myfile"]["tmp_name"][$i], $url); // 压缩图片 $image = new ImgCompress($url, 1); $image->compressImg($small_url); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 total 获取到文件数量后循环把文件转存,存本地,存图片服务器都可以; 压缩的话,使用ImgCompress压缩,ImgCompress是网上分享的一个压缩类,亲测好用,而且压缩效果不错。 ImgCompress 源码: class ImgCompress { private $src; private $image; private $imageinfo; private $percent=0.5; /* param $src源图 param $percent压缩比例 */ public function __construct($src,$percent=1) { $this->src = $src; $this->percent = $percent; } /* param string $saveName 图片名(可不带扩展名用原图名)用于保存。或不提供文件名直接显示 */ public function compressImg($saveName='') { $this->_openImage(); if(!empty($saveName)) { $this->_saveImage($saveName);//保存 } else { $this->_showImage(); } } /* 内部:打开图片 */ private function _openImage() { list($width, $height, $type, $attr) = getimagesize($this->src); $this->imageinfo = array( 'width'=>$width, 'height'=>$height, 'type'=>image_type_to_extension($type,false), 'attr'=>$attr ); $fun = "imagecreatefrom".$this->imageinfo['type']; $this->image = $fun($this->src); $this->_thumpImage(); } /** * 内部:操作图片 */ private function _thumpImage() { $new_width = $this->imageinfo['width'] * $this->percent; $new_height = $this->imageinfo['height'] * $this->percent; $image_thump = imagecreatetruecolor($new_width,$new_height); //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度 imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']); imagedestroy($this->image); $this->image = $image_thump; } /** * 输出图片:保存图片则用saveImage() */ private function _showImage() { header('Content-Type: image/'.$this->imageinfo['type']); $funcs = "image".$this->imageinfo['type']; $funcs($this->image); } /** * 保存图片到硬盘: * @param string $dstImgName 1、可指定字符串不带后缀的名称,使用源图扩展名 。2、直接指定目标图片名带扩展名。 */ private function _saveImage($dstImgName) { if(empty($dstImgName)) return false; $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif']; //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名 $dstExt = strrchr($dstImgName ,"."); $sourseExt = strrchr($this->src ,"."); if(!empty($dstExt)) $dstExt =strtolower($dstExt); if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt); //有指定目标名扩展名 if(!empty($dstExt) && in_array($dstExt,$allowImgs)) { $dstName = $dstImgName; } elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)) { $dstName = $dstImgName.$sourseExt; } else { $dstName = $dstImgName.$this->imageinfo['type']; } $funcs = "image".$this->imageinfo['type']; $funcs($this->image,$dstName); } /** * 销毁图片 */ public function __destruct() { imagedestroy($this->image); } } 原文链接:https://blog.csdn.net/a437629292/article/details/81224133