1. 判斷對文件空目錄是否有讀寫許可權的php代碼
is_writable用來處理,記住 PHP 也許只能以運行 webserver 的用戶名(通常為 \'nobody\')來訪問文件。不計入安全模式的限制。
Example #1 is_writable() 例子
復制代碼代碼如下:
<?php
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
?>
2. 【php學習】PHP中判斷目錄是否為空的函數
大體就是這樣的:function is_dir_null($dir){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//var_mp($dh);
$s='';
while (($file = readdir($dh))) {
if($file!='.' && $file!='..'){
$s.="<br/>".$file."<br/>";
}
}
if($s==''){
return "該目錄為空";
}else{
return "目錄中有內容".$s;
}
closedir($dh);
}
}}
echo is_dir_null("./");
3. php判斷是否為文件目錄方法
is_dir
(PHP 4, PHP 5, PHP 7)
is_dir — 判斷給定文件名是否是一個目錄
1.說明
is_dir ( string $filename ) : bool
判斷給定文件名是否是一個目錄。
2.參數
filename
如果文件名存在並且為目錄則返回 TRUE。如果 filename 是一個相對路徑,則按照當前工作目錄檢查其相對路徑。 If filename is a symbolic or hard link then the link will be resolved and checked.If you have enabled open_basedir further restrictions may apply.
3.返回值
如果文件名存在,並且是個目錄,返回 TRUE,否則返回FALSE。
4.範例如下圖
代碼展示
4. 如何用PHP判斷一個文件夾是否為空
$a=array_diff(scandir($dir),array('..','.'));
//數組為空代表文件夾為空
5. PHP判斷當前目錄下txt文件中是否存在指定的內容
file_get_contents(filename),這個函數,可以吧文件的內容放入一個變數$str中,然後使用stripos() 函數查找字元串在$str中第一次出現的位置(不區分大小寫)。返回字元串在$str中第一次出現的位置,如果沒有找到字元串則返回 FALSE。
6. php中如何判斷一個目錄有沒有文件
<?php
//自定義一個遍歷目錄的函數,注意目錄中的目錄。
functionrmdi_r($dirname){
//判斷是否為一個目錄,非目錄直接關閉
if(is_dir($dirname)){
//如果是目錄,打開他
$name=opendir($dirname);
//使用while循環遍歷
while($file=readdir($name)){
//去掉本目錄和上級目錄的點
if($file=="."||$file==".."){
continue;
}
//如果目錄裡面還有一個目錄,再次回調
if(is_dir($dirname."/".$file)){
rmdi_r($dirname."/".$file);
}
如果目錄裡面是個文件,那麼輸出文件名
if(is_file($dirname."/".$file)){
echo($dirname."/".$file);
}
}
//遍歷完畢關閉文件
closedir($name);
//輸出目錄名
echo($dirname);
}
}
//調用函數
rmdi_r("這里填寫需要遍歷某文件夾的絕對路徑");
7. php中判斷文件空目錄是否有讀寫許可權
is_writable用來處理,記住 PHP 也許只能以運行 webserver 的用戶名(通常為 \'nobody\')來訪問文件。不計入安全模式的限制。
Example #1 is_writable() 例子
復制代碼代碼如下:
<?php
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
?>
8. php判斷目錄是否存在
file_exists — 檢查文件或目錄是否存在
說明
bool file_exists ( string $filename )
檢查文件或目錄是否存在。
參數
filename
文件或目錄的路徑。
在 Windows 中要用 //computername/share/filename 或者 \computernamesharefilename 來檢查網路中的共享文件。
返回值
如果由 filename 指定的文件或目錄存在則返回 TRUE,否則返回 FALSE。
Note:
This function will return FALSE for symlinks pointing to non-existing files.
Warning
如果因為安全模式的限制而導致不能訪問文件的話,該函數會返回 FALSE。然而,可以使用 include 來包含,如果文件在 safe_mode_include_dir 所指定的目錄里。
Note:
The check is done using the real UID/GID instead of the effective one.
Note: 因為 PHP 的整數類型是有符號整型而且很多平台使用32位整型, 對2GB以上的文件,一些文件系統函數可能返回無法預期的結果 。
範例
Example #1 測試一個文件是否存在
<?php
$filename='/path/to/foo.txt';
if(file_exists($filename)){
echo"文件$filename存在";
}else{
echo"文件$filename不存在";
}
?>
//以上內容來自官方PHP開發幫助文檔
9. php怎麼判斷是否有某個文件夾
php 語言提供了 file_exists 函數,其功能是:
file_exists—檢查文件或目錄是否存在
函數原型定義如下:
boolfile_exists(string$filename)
示例代碼:
<?php
$filename='/path/to/';
if(file_exists($filename)){
echo"$filenameexists";
}else{
echo"$filenamedoesnotexist";
}
?>
10. php中判斷文件空目錄是否有讀寫許可權的函數代碼
is_writable用來處理,記住
PHP
也許只能以運行
webserver
的用戶名(通常為
\'nobody\')來訪問文件。不計入安全模式的限制。
Example
#1
is_writable()
例子
復制代碼
代碼如下:
<?php
$filename
=
'test.txt';
if
(is_writable($filename))
{
echo
'The
file
is
writable';
}
else
{
echo
'The
file
is
not
writable';
}
?>
上面的函數有一個問題就是filename
必需。規定要檢查的文件
,必須是文件啊,目錄不可判斷,下面我們來判斷空目錄。
實例1
該功能非常常用,特別在一些需要生成靜態文件的項目中,一個目錄是否可以,關乎到是否對該目錄有創建文件刪除文件的許可權
復制代碼
代碼如下:
/*
問題出現:如何檢查一個目錄是否可寫,如何目錄下還有目錄和文件,那麼都要檢查
思路:
(1)首先先寫出檢查空目錄是否可寫的演算法:
在該目錄中生成一個文件,如果不能生成,表明該目錄沒有寫的許可權
(2)使用遞歸的辦法來進行檢查
代碼實現:
*/
set_time_limit(1000);
function
check_dir_iswritable($dir_path){
$dir_path=str_replace('\','/',$dir_path);
$is_writale=1;
if(!is_dir($dir_path)){
$is_writale=0;
return
$is_writale;
}else{
$file_hd=@fopen($dir_path.'/test.txt','w');
if(!$file_hd){
@fclose($file_hd);
@unlink($dir_path.'/test.txt');
$is_writale=0;
return
$is_writale;
}
$dir_hd=opendir($dir_path);
while(false!==($file=readdir($dir_hd))){
if
($file
!=
"."
&&
$file
!=
"..")
{
if(is_file($dir_path.'/'.$file)){
//文件不可寫,直接返回
if(!is_writable($dir_path.'/'.$file)){
return
0;
}
}else{
$file_hd2=@fopen($dir_path.'/'.$file.'/test.txt','w');
if(!$file_hd2){
@fclose($file_hd2);
@unlink($dir_path.'/'.$file.'/test.txt');
$is_writale=0;
return
$is_writale;
}
//遞歸
$is_writale=check_dir_iswritable($dir_path.'/'.$file);
}
}
}
}
return
$is_writale;
}
上面實例主要是fopen去在目錄創建文件或在文件中寫內容,這樣就可以判斷目錄的讀寫許可權了。