『壹』 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;
}
?