php中文件操作常用函数有哪些
php中文件操作常用函数有哪些
一、总结
一句话总结:读写文件函数 判断文件或者目录是否存在函数 创建目录函数
file_exists() mkdir() file_get_content() file_put_content()
1、php检查文件或者目录是否存在函数是什么?
file_exists()
file_exists — 检查文件或目录是否存在
<?php
$filename = '/path/to/foo.txt'; if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
2、php创建一个目录的函数是什么?
mkdir() $ans=mkdir($file_path,0777,true);
mkdir — 新建目录
$ans=mkdir ($file_path,0777,true);
二、php中如何创建文件夹
参考:php中如何创建文件夹
https://www.cnblogs.com/modou/p/5991365.html
这个功能比较简单,直接上代码了:
$dir = iconv("UTF-8", "GBK", "Public/bookcover");
if (!file_exists($dir)){
mkdir ($dir,0777,true);
echo '创建文件夹bookcover成功';
} else {
echo '需创建的文件夹bookcover已经存在';
}
iconv方法是为了防止中文乱码,保证可以创建识别中文目录,不用iconv方法格式的话,将无法创建中文目录
mkdir方法的第一个参数是要创建的目录路径,第二个参数是指创建目录的权限,在windows系统下该参数会被忽略,第三个参数是指是否创建多级目录,默认为false
三、php 判断文件或目录是否存在
参考:php 判断文件或目录是否存在
https://www.cnblogs.com/diony/p/5541458.html
判断文件或目录是否存在有自带的函数
file_exists:文件是否存在
$file = "check.txt";
if(file_exists($file))
{
echo "当前目录中,文件".$file."存在";
}
else
{
echo "当前目录中,文件".$file."不存在";
}
is_dir:目录是否存在
$dir = "c:/datacheck";
if(is_dir($dir))
{
echo "当前目录下,目录".$dir."存在";
}
else
{
echo "当前目录下,目录".$dir."不存在";
}
四、thinkphp5自写的文件缓存代码
//5、【下一题】按钮逻辑---确定这道题目开始的做题时间
public function question_next(){
$back_data=[];
$back_data['operation_success']=false;
if(request()->isAjax()){
$question_index_num=input('question_index_num');
$dataIn=[];
$dataIn['space_type']=input('space_type');
$dataIn['xiulian_type']=input('xiulian_type');
$dataIn['sort_type']=input('sort_type');
$dataIn['time_type']=input('time_type'); //2.1、取出缓存
$question_list_path='data_record/blog_question_record/'.$dataIn['space_type'].'/sort_type_'.$dataIn['sort_type'].'/time_'.$dataIn['time_type'].'/xiulian_type_'.$dataIn['xiulian_type'].'.php';
if(file_exists($question_list_path)) {
$back_data['operation_success']=true;
$question_list = file_get_contents($question_list_path);
$question_list = json_decode($question_list, true); //2.2、确定题目的开始时间
$question_list[$question_index_num]['bq_question_begin_time']=time();
$back_data['bq_question_begin_time']=time();
//2.4、提交缓存
$createDirAns=\app\index\model\tools\file\Directory::createDir($question_list_path);
if($createDirAns){
$result=file_put_contents($question_list_path,json_encode($question_list));
//dump($result);
$back_data['cache_result']=$result;
}
} return $back_data;
}
return $back_data;
}
<?php
namespace app\index\model\tools\file;
use app\index\model\Base; class Directory extends Base
{
//1、创建指定目录的文件夹
public static function createDir($file_path){ $dir = iconv("GBK", "UTF-8", "$file_path");
//步骤一:去掉文件路径的文件名,只留下文件名
$file_arr=explode('/',$file_path);
//1、去掉为空的
$file_arr_new=[];
foreach ($file_arr as $key=>$val){
if(strlen($val)>=1) $file_arr_new[]=$val;
}
$file_arr=$file_arr_new;
array_pop($file_arr);
$file_path=implode('/',$file_arr);
if (!file_exists($file_path)){
$ans=mkdir ($file_path,0777,true);
if($ans===false) return false;
return true;
} else {
return true;
}
}
}
php中文件操作常用函数有哪些的更多相关文章
- PHP的文件操作常用函数
PHP文件操作 1 获得文件名:basename - 返回路径中的文件名部分 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名.如果文件名是以 suffix 结束的,那这一部分也会被 ...
- PHP文件操作常用函数总结
一 .解析路径: 1 获得文件名: basename(); 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名.如果文件名是以 suffix 结束的,那这一部分也会被去掉. eg: $ ...
- Delphi7文件操作常用函数
1. AssignFile.Erase AssignFile procedure AssignFile(var F; FileName: string);:给文件变量连接一个外部文件名.这里需要注意的 ...
- python 文件操作: 文件操作的函数, 模式及常用操作.
1.文件操作的函数: open("文件名(路径)", mode = '模式', encoding = "字符集") 2.模式: r , w , a , r+ , ...
- python 文件操作的函数
1. 文件操作的函数 open(文件名(路径), mode="?", encoding="字符集") 2. 模式: r, w, a, r+, w+, a+, r ...
- 【PHP】最详细PHP从入门到精通(三)——PHP中的数组常用函数汇总
PHP从入门到精通 之PHP中的数组常用函数详解 数组作为PHP中最常用的结构之一,PHP强大的数组函数功能,给数组的相关操作带来了极大的便利.今天给大家介绍的PHP中数组函数,是PHP数组中重要的 ...
- go语言之进阶篇字符串操作常用函数介绍
下面这些函数来自于strings包,这里介绍一些我平常经常用到的函数,更详细的请参考官方的文档. 一.字符串操作常用函数介绍 1.Contains func Contains(s, substr st ...
- C#文件操作常用相关类(Directory类、File类、Path类)
1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
随机推荐
- 【题解】Luogu P3901 数列找不同
我博客中对莫队的详细介绍 原题传送门 不错的莫队练手题 块数就直接取sqrt(n) 对所有询问进行排序 排序第一关键词:l所在第几块,第二关键词:r的位置 考虑Ai不大,暴力开数组 add时如果加之后 ...
- 【题解】Luogu P1972 [SDOI2009]HH的项链
原题传送门 莫队入门题 我博客里对莫队的介绍 很多人说这题卡莫队,但窝随便写了一个程序就过了qaq(虽说开了氧化) 我们在排序询问时,普通是这样qaq inline bool cmp(register ...
- phpstorm 一个窗口打开多个项目
参考:https://imshusheng.com/php/135.html 文件 -> 设置 -> 项目"XXX" -> Directories- > A ...
- tar 压缩指令基本用法
压缩:tar -cjvf aaa.tar.bz2 www.test.com/ --exclude *.log(-j是用bz2压缩,-exclude是排除.log后缀的文件) c-创建 j-bzip ...
- adb驱动安装和使用报错笔记
adb驱动安装 adb驱动下载地址:https://adb.clockworkmod.com/ 安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装 ...
- ThreadPoolExecutor线程池
为什么使用线程池: 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率. 2.线程并发数量过多,抢占系统资源从而导致阻塞. 3.对线程进行一些简单的管理. 在java ...
- HDU 2647 Reward 【拓扑排序反向建图+队列】
题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...
- 2199: [Usaco2011 Jan]奶牛议会 2-sat
链接 https://www.luogu.org/problemnew/show/P3007 https://www.lydsy.com/JudgeOnline/problem.php?id=2199 ...
- 【论文笔记】Spatial Temporal Graph Convolutional Networks for Skeleton-Based Action Recognition
Spatial Temporal Graph Convolutional Networks for Skeleton-Based Action Recognition 2018-01-28 15:4 ...
- HDU 5829 Rikka with Subset(NTT)
题意 给定 \(n\) 个数 \(a_1,a_2,\cdots a_n\),对于每个 \(K\in[1,n]\) ,求出 \(n\) 个数的每个子集的前 \(K\) 大数的和,输出每个值,对 \(99 ...