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去在目录创建文件或在文件中写内容,这样就可以判断目录的读写权限了。