• touch()          创建文件 (修改时间,不存在时创建)
  • copy()            复制文件,复制过程中可以修改文件名
  • rename()        重命名 或  移动文件  不能移动目录
  • unlink()           删除文件
  • file_exists()      判断文件是否存在
  • filesize()         获取文件大小
  • is_file()           判断是否是文件
  • fopen()           以什么方式打开文件,第二参数以什么方式打开    r w a(追加写入)  x(文件不存在新建,存在则报错,异或)
  • fclose()          关闭文件
  • fread()             读取文件内容,
  • feof()             判断指针是否读到文件结尾
  • fwrite()
  • file()               把整个文件读入一个数组中,file ( string $filename [, int $flags = 0 [, resource $context ]] ) : array,Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file()returns FALSE.
  • file_put_contents()               FILE_APPEND(追加写入)
  • file_get_contents()               字符串形式获取文件的内容

目录相关

  • opendir()
  • readdir()
  • closedir()
  • mkdir()                            想要嵌套创建目录,需要加参数                    第二个 0777    第三个true表示允许嵌套创建
  • rmdir()           只能删除空目录
  • is_dir()

文件上传

  1. 从客户端选择文件,通过网络上传到服务器,为了防止病毒等,会把文件先存在服务器的临时目录tmp中(在这个临时目录中的文件都是不能执行的),当脚本运行结束时,临时目录中的文件会被删除。所以需要在脚本结束前,把上传的文件从临时目录中移动到指定的目录中,才成功实现文件上传。
  2. 文件上传表单需要加上   enctype="multipart/form-data"   ,文件信息存储在$_FILES超全局数组,是个二维数组
  3. 上传文件考虑几个问题: (1)根据错误号  error  判断文件是否成功上传到服务器的临时目录中;
    (2)文件的类型是否符合要求
    (3)文件大小是否符合要求
    (4)上传多个文件时,可能会出现重名,为避免重名需要给文件重命名;
    (5)创建保存文件的目录
    (6)然后把文件从临时目录中移到新目录中保存
  4. 文件上传相关的配置选项:
    upload_max_filesize    允许上传的文件最大值
    file_uploads=on            允许文件上传
    upload_tmp_dir            文件上传的临时目录
    max_file_uploads        允许上传的文件个数  (php.ini 默认配置是20,所以即便后端接收了40个文件,但最后上传成功的一定是20个。想要上传成功80个,需要将此项改为80,同时要注意文件的大小限制)
    post_max_size             post最大传输内容(包括文件还有其他信息)
    max_excution_time      文件上传的时间限制,  0表示没有限制,   # 默认设置30秒, 这设置了脚本被解析器中止之前允许的最大执行时间,单位秒。 这有助于防止写得不好的脚本占尽服务器资源。在 安全模式 下你不能通过 ini_set() 来修改此设置。 唯一的解决方法是关闭安全模式或者在 php.ini 中修改时间限制。
    max_input_time           脚本解析输入数据(类似 POST 和 GET)允许的最大时间,单位是秒。 它从接收所有数据到开始执行脚本进行测量的.
  5. 在lnmp环境下, PHP上传文件失败, 基本上就几个原因:
    (1) upload_max_filesize设置的值过小, 导致文件上传不上去, 服务器端打印$_FILES数组就会是null   # 默认只有2m  还发现一点: 文件大小的限制是针对多个文件的, 比如说上传两个文件, 那么 如果这两个文件的总和大于了配置文件设置的值, 仍然会上传失败
    (2) post_max_size 设置的过小  # 默认只有8m
    (3) upload_tmp_dir 设置了自定义的目录, 却没有赋予这个目录读写权限,  # 这个默认是注释掉的, 默认使用系统的临时目录,  具体是哪个目录还没研究...  如果是注释掉的话, 那么一般情况下是没有问题的, (找到了, Linux系统 cd / 在根目录下有一个tmp目录,嗯, 应该就是他了)
    (4) memory_limit  内存不够用,  # 不过系统默认值为 -1 ,就是不限制,  可以设置其他值   (https://segmentfault.com/q/1010000010591073)这个里面说了一个stripos函数导致文件上传内存会翻倍的问题, 需要memory_limit设置更大.
    (5) nginx的配置文件没有设置client_max_body_size, 需要设置 这个值, 比如 client_max_body_size  100m;   
    (6) 其余的几个相关的参数都是系统默认的 file_uploads=on  #默认开启, 排查的时候也要看一下, 万一系统默认关闭

文件下载

(1)header('content-type:image/jepg');   //指定下载的文件 类型  当不知道要下载的文件类型时,省略这行代码
(2)header('content-disposition:attachment;filename="abc.jpeg" ');    //对下载文件进行描述,并指定文件下载后的名称
(3)readfile('./statics/a.jpeg');   //,给定路径,读取要下载的文件

当浏览器是IE的时候,可能出现文件名乱码,需要再写一行代码     iconv('转换前的字符集','想要转换的字符集','要转换的内容');

自定义文件上传函数(多文件上传时,需要在表单加上  multiple="multiple"     name属性值设置数组)

 <?php

 /**
* @param 文件上传页面file表单的name值
* @param 上传文件后的新目录
* @param 文件类型
* @param 文件大小
* @return 上传后的信息
*/
//文件上传函数就是处理客户端上传到服务器临时目录的文件
function uploadFile($fileName,$path='./uploads',$arr=['image/jpeg','image/png','image/jpg'],$filesize=1000000)
{
$file = $_FILES[$fileName];
if($file['error']>0)
switch($file['error']){
case 1:
case 2:
return '文件过大';
case 3:
return '上传文件不完整,请重新上传';
case 4:
return '请选择文件';
case 6:
return 'tmp不存在';
case 7:
return '没有权限';
case 8:
return '........';
}
//判断文件类型是否合法
if(!in_array($file['type'], $arr)){
return '上传的文件类型不合法';
}
//判断文件大小
if($file['size']>$filesize){
return '上传文件过大';
} //创建存放文件的新目录
if(!file_exists($path)){
mkdir($path);
}
//把文件重命名,,,,,想了想必须得先创建目录,不然没法判断文件名是否已存在,给他一个文件路径file_exists才能找到文件
//先获取文件后缀
$suffix = strrchr($file['name'],'.');
//如果传的$path参数后面带有/或\,需要删除掉
$path = rtrim($path,'/\\');
do{
//生成一个新的文件名,并拼接后缀
$newFileName = md5(mt_rand(1000,9999).uniqid()).$suffix;
$newPath = $path.'/'.$newFileName;
}while(file_exists($newPath)); //移动到新目录,使用move_uploaded_file还可以判断是不是post传过来的
if(move_uploaded_file($file['tmp_name'],$newPath)){
return '上传文件成功';
}else{
return '上传失败,请重新上传';
}
}
$res = uploadFile('pic','./statics/');
echo '<pre>';
print_r($res);

文件上传类(多文件或单文件都可以)

 <?php
//能够实现多文件上传,也可以单文件上传
class Upload
{
//文件上传表单的name属性值
private $fileName;
//文件上传后,创建的保存路径
private $path;
//文件的类型
private $type;
//文件大小
private $size;
//用于接收遍历后的文件
private $files = []; //初始化 注意:如果用户传的路径是嵌套的,需要给mkdir()传第二个和第三个参数
public function __construct($fileName,$path='./statics/images',$size=1500000,array $type=['image/jpeg','image/png','image/jpg','image/gif'])
{
$this->fileName = $fileName;
$this->path = $path;
$this->type = $type;
$this->size = $size;
} //为避免可能出现用户上传的是单文件却调用多文件处理方法的错误,新进行判断用户上传的文件是不是三维数组is_array($_FILES['pic']['name'])
public function uploadFile()
{
//这个判断也可可以写在uploads 和 upload 方法里判断
if(is_array($_FILES['pic']['name'])){
echo $this->uploads(); //输出信息
}else{
echo $this->upload();
}
} //用户上传多文件时调用此方法。
public function uploads()
{
//其实多文件上传时,就是遍历一遍,然后在每次遍历的时候,把error,type,size都判断一次
//每遍历一次就把所有单文件上传的步骤执行一次
foreach($_FILES[$this->fileName]['name'] as $k=>$v){
$this->files['name'] = $v;
$this->files['type'] = $_FILES[$this->fileName]['type'][$k];
$this->files['tmp_name'] = $_FILES[$this->fileName]['tmp_name'][$k];
$this->files['error'] = $_FILES[$this->fileName]['error'][$k];
$this->files['size'] = $_FILES[$this->fileName]['size'][$k];
//把那些方法都调用一遍,判断必须判断类型,否则返回的错误信息也会转换成真
if($this->fileError()!==true){
echo $this->fileError(); //输出返回的错误信息
}elseif($this->fileType()!==true){
echo $this->fileType();
}elseif($this->fileSize()!==true){
echo $this->fileSize();
}else{
echo $this->moveImg();
}
}
} //当用户是单文件上传时,调用此方法
public function upload()
{
//单文件上传把所有步骤都写在这一个方法里
$file = $_FILES[$this->fileName];
//判断错误号
if($file['error']>0){
switch($file['error']){
case 1:
case 2:
return '文件过大';
case 3:
return '上传文件不完整,请重新上传';
case 4:
return '请选择文件';
case 6:
return 'tmp不存在';
case 7:
return '没有权限';
}
}
//判断文件类型
if(!in_array($file['type'],$this->type)){
return '上传文件类型不合法';
}
//判断文件大小
if($file['size']>$this->size){
return '上传文件过大,请压缩后上传';
}
//判断保存文件的目录是否存在
if(!file_exists($this->path)){
mkdir($this->path,0777,true); //可以递归创建目录
}
//过滤用户可能传进的/ \
$this->path = rtrim($this->path,'/\\');
//获取文件后缀
$suffix = strrchr($file['name'],'.');
do{
$newPath = $this->path.'/'.md5(time().mt_rand(100,999).uniqid()).$suffix;
}while(file_exists($newPath));
if(move_uploaded_file($file['tmp_name'],$newPath)){
return '<font color="green">上传文件成功</font>';
}else{
return '<font color="red">上传文件失败</font>';
}
} //判断错误号
private function fileError()
{
if($this->files['error']>0){
switch($this->files['error']){
case 1: return '上传的文件超过了 php.ini 中 upload_max_filesize选项限制的值';
case 2: return '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
case 3: return '文件只有部分被上传';
case 4: return '没有文件被上传';
case 6: return '找不到临时文件夹';
case 7: return '文件写入失败';
}
}
return true;
} //判断文件类型是否合法
private function fileType()
{
if(!in_array($this->files['type'],$this->type)){
return '上传类型不合法';
}
return true;
} //判断上传文件大小是否合法
private function fileSize()
{
if($this->files['size']>$this->size){
return '上传文件过大,请压缩后上传';
}
return true;
} //从临时目录中移出
private function moveImg()
{
//判断保存文件的目录是否存在
if(!file_exists($this->path)){
mkdir($this->path,0777,true);
}
//用户传路径的时候可能会多/ \ 过滤掉
$this->path = rtrim($this->path,'/\\');
//获取文件后缀
$suffix = strrchr($this->files['name'],'.');
//重命名文件
do{
$newPath = $this->path.'/'.md5(time().mt_rand(100,999).uniqid()).$suffix;
}while(file_exists($newPath));
//移动文件
if(move_uploaded_file($this->files['tmp_name'],$newPath)){
return '<font color="green">上传文件成功</font>';
}else{
return '<font color="red">上传文件失败</font>';
}
}
}

验证码类

<?php
class Code{
private $width;
private $height;
private $codeNum;
private $fontFamily;
private $image;
private $font; //成员属性
function __construct($fontFamily='',$width=100,$height=40,$codeNum=4){
session_start();
$this->fontFamily=$fontFamily;
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
} function __tostring(){
$this->getCreateImg();
$this->setPixel();
$this->setLine();
$this->setChar();
$this->outputImg();
$_SESSION['code']=$this->font;
return '';
} private function getCreateImg(){
$this->image=imagecreatetruecolor($this->width,$this->height);
$back=imagecolorallocate($this->image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagefill($this->image,0,0,$back);
$borderColor=imagecolorallocate($this->image,255,0,0);
imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$borderColor); } private function setPixel(){
for($i=0;$i<299;$i++){
$pixelColor=imagecolorallocate($this->image,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));
imagesetpixel($this->image,mt_rand(2,$this->width-2),mt_rand(2,$this->height-2),$pixelColor);
}
} private function setLine(){
for($i=0;$i<5;$i++){
$lineColor=imagecolorallocate($this->image,mt_rand(180,200),mt_rand(180,200),mt_rand(180,200));
imageline($this->image,mt_rand(2,$this->width-2),mt_rand(2,$this->height-2),mt_rand(2,$this->width-2),mt_rand(2,$this->height-2),$lineColor);
}
} private function setChar(){
$str='3456789ABCDEFGHJKLMNPQRSTUVWXYabcdefghijkmnpqrstuvwxyz';
for($i=0;$i<$this->codeNum;$i++){
$this->font.=$str{mt_rand(0,strlen($str)-1)};
}
if($this->fontFamily==''){
for($i=0;$i<strlen($this->font);$i++){
$fontColor=imagecolorallocate($this->image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
$x=$this->width/$this->codeNum*$i+mt_rand(3,7);
$y=mt_rand(10,$this->height/2);
imagechar($this->image,mt_rand(3,5),$x,$y,$this->font{$i},$fontColor); }
}else{
for($i=0;$i<strlen($this->font);$i++){
$fontColor = imagecolorallocate($this->image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
$x = $this->width/$this->codeNum * $i + mt_rand(5,8);
$y = mt_rand($this->height/2,$this->height);
imagettftext($this->image,mt_rand($this->height/3,$this->height/2),mt_rand(0,45),$x,$y,$fontColor,$this->fontFamily,$this->font{$i});
}
}
} private function outputImg(){
header('Content-type:image/jpeg');
imagejpeg($this->image);
} //6.释放资源 析构方法
function __destruct(){
//imagedestroy($this->image);
//imagedestroy($this->image);
imagedestroy($this->image);
}
}

Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file()returns FALSE.

Note:

最新文章

  1. Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构
  2. VMware 12Pro 安装MACOS 10.10
  3. C#伪静态实现的方法
  4. Cygwin之SSH服务安装过程问题
  5. Hyper Prefix Sets
  6. python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中
  7. matlab 2014a 改为英文版本号
  8. pyspark简要原则
  9. HDU 1532 Drainage Ditches
  10. HashMap面试题:90%的人回答不上来
  11. Python 函数返回值
  12. MyEclipse无法部署项目
  13. java之servlet小记
  14. STL之vector容器详解
  15. 状态模式-State-订单状态
  16. JPush Flutter Plugin(Futter推送-极光推送)
  17. Linux (rz、sz命令行)与本地电脑 命令行上传、下载文件
  18. can&#39;t load package the specified module could not be found
  19. 一步一步pwn路由器之radare2使用全解
  20. WiFi安全那些事儿,整理推荐~

热门文章

  1. PHPstorm配置同步服务器文件
  2. django form组件 cookies,session
  3. Spring 的 Bean 管理(XML 方式)
  4. 怎样在 Vue 的 component 组件中使用 props ?
  5. spring 多数据源配置
  6. Mongoose 使用Node操作MongoDB
  7. 用Python输出一个Fibonacci数列
  8. kubernetes资源清单之DaemonSet
  9. markdown实现点击链接下载文件
  10. python常用模块:sys、os、path、setting、random、shutil