php文件是否存在|php判断本地文件是否存在

|

① 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开发帮助文档

② php如何实现判断文件是否存在后跳转

file_exists (PHP 3, PHP 4 )file_exists — 检查文件或目录是否存在 说明 bool file_exists ( string filename)如果由 filename 指定的文件或目录存在则返回回 TRUE,否则返回 FALSE。在 Windows 中要用 //computername/share/filename 或者 \\computername\share\filename 来检查网络中答的共享文件。例子 1. 测试一个文件是否存在<?php $filename = '/path/to/foo.txt';if (file_exists($filename)) {print "The file $filename exists"; } else {print "The file $filename does not exist"; } ?>

③ php中如何判断一个目录有没有文件

/***判断是否为空目录*@param$pathstring指定的路径*@returnbool非空目录和非目录均返回false*/functionis_empty_dir($path){if(!is_dir($path)){returnfalse;}$dir=opendir($path);while(false!==($file=readdir($dir))){if(!in_array($file,['.','..'])){closedir($dir);returnfalse;}}closedir($dir);returntrue;}

④ php中怎样判断一个文件是否存在

$file_name = "info_check.exe";$file_dir = "/public/www/download/";if (!file_exists($file_dir . $file_name)) { //检查文件是否存在echo "文件找不到";exit; }

⑤ php判断本地文件是否存在

PHP文件编码导致的问题.

<?php$s='C:UsersAdministratorPictures狗.jpg';var_mp(file_exists($s));

在 使用 ANSI 编码的情况下. 上述代码. 在Windows环境下执行成回功.

如果换答成 utf-8 编码. 则会输出 false .

改成以下代码. 则在 utf-8 编码下 运行正常

<?php$s='C:UsersAdministratorPictures狗.jpg';var_mp(file_exists(mb_convert_encoding($s,'gbk','utf-8')));

⑥ php判断文件夹是否存在不存在则创建

php判断文件夹是否存在用到的工具:notepad++,代码如下:

functionmkdirs($a1,$mode=0777){if(is_dir($a1)||@mkdir($al,$mode))returnTRUE;if(!mkdirs(dirname($a1),$mode))returnFALSE;[email protected]($a1,$mode);}mkdirs("a1");

说明:程序判断a1这个目录是否存在,如果存在就返回真,如果不存在就创建a1目录,默认给与读写和执行的权限。

注意事项:777权限适用于linux环境。

⑦ php怎么判断是否有某个文件夹

php 语言提供了 file_exists 函数,其功能是:

file_exists—检查文件或目录是否存在

函数原型定义专如下:

boolfile_exists(string$filename)

示例代码属:

<?php$filename='/path/to/';if(file_exists($filename)){echo"$filenameexists";}else{echo"$filenamedoesnotexist";}?>

⑧ php如何判断文件是否存在,包括本地和远程文件

当检查的文件是本地时用php自带的file_exists检查就行了,而此函数只能检查本地的函数是否存在,所以如果要检查远程的文件是否存在只能用其它的方法了。如果所服务器中php的配置开启了“allow_url_fopen = On”,即允许远端访问,那么也很简单,其实这个是php.ini中默认开启的,用fopen函数判断就行了,能打开说明存在如果allow_url_fopen = Off那么可以用socket通讯来解决下面写的一个通用函数my_file_exists来检查文件是否存在function my_file_exists($file){if(preg_match('/^http:\/\//',$file)){//远程文件if(ini_get('allow_url_fopen')){if(@fopen($file,'r')) return true;}else{$parseurl=parse_url($file);$host=$parseurl['host'];$path=$parseurl['path'];$fp=fsockopen($host,80, $errno, $errstr, 10);if(!$fp)return false;fputs($fp,GET {$path} HTTP/1.1 \r\nhost:{$host}\r\n\r\n);现在就可以调用此函数来检查文件的存在性,而不用去考虑是远程还是本地文件,或者是否禁用了allow_url_open


赞 (0)