『壹』 php 判断文件是否存在
<?php
/*
*2014年3月21日18:06:02
*@param$fileNameString文件或者文件夹名称
*@autherbyLAMP兄弟连65期版洪卫
*/
if(file_exists($fileName))
{
return"存在权";
}
else
{
return"不存在";
}
『贰』 php判断文件夹是否存在不存在则创建
//直接这样即可:
$dir='./test/test';
is_dir($dir)ORmkdir($dir,0777,true);//如果文件夹不存在,将以递归方式创建该文件夹
『叁』 php判断文件夹或文件是否存在,及不存在时如何创建
如果文抄件夹不存在直接袭创建:
$folder='test';
is_dir($folder)ORmkdir($folder,0777,true);
文件不存在直接打开文件就创建了
$file='index.php';
is_file($file)ORfclose(fopen($file,'w'));
『肆』 php 检测文件是否存在的几种方式
一、 file_exists();
二、is_file();
$file='test';
file_exists($file)ORexit('该目录不存在');
is_file($file)ORexit('该目录不存在');
file_exists 既可以用来检查文件夹,也可以用来检查文件
is_file 只能用来检查文件
『伍』 php检测文件夹下是否还有文件
php判断文件还是文件夹主要通过is_file跟is_dir函数判断,下面分别讲解:
is_file()函数
is_file()函数 用来判断是否为文件,返回结果为true或者false
举例:
$ifile="c:/test";
$result=is_file($ifile);
echo $result;
输出:false
is_dir()函数
is_dir()函数用来判断是否为目录,返回结果为true或者false
举例:
$ifile="c:/test";
$result=is_file($ifile);
echo $result;
输出:true
『陆』 PHP判断远程文件是否存在
?php
/*
函数:remote_file_exists
功能:判断远程文件是否存在
参数: $url_file -远程文件URL
返回:存在返回true,不存在或者其他原因返回false
*/
function remote_file_exists($url_file){
//检测输入
$url_file = trim($url_file);
if (empty($url_file)) { return false; }
$url_arr = parse_url($url_file);
if (!is_array($url_arr) || empty($url_arr)){return false; }
//获取请求数据
$host = $url_arr['host'];
$path = $url_arr['path'] ."?".$url_arr['query'];
$port = isset($url_arr['port']) ?$url_arr['port'] : "80";
//连接服务器
$fp = fsockopen($host, $port, $err_no, $err_str,30);
if (!$fp){ return false; }
//构造请求协议
$request_str = "GET ".$path."HTTP/1.1/r/n";
$request_str .= "Host:".$host."/r/n";
$request_str .= "Connection:Close/r/n/r/n";
//发送请求
fwrite($fp,$request_str);
$first_header = fgets($fp, 1024);
fclose($fp);
//判断文件是否存在
if (trim($first_header) == ""){ return false;}
if (!preg_match("/200/", $first_header)){
return false;
}
return true;
}
?