PHP中is_file()函數的定義與使用方法

墨辰丷
發布: 2023-03-31 12:54:01
原創
3890 人瀏覽過

php中is_file()函數是用來判斷檔案是否存在,使用方法也非常的簡單,有需要的小夥伴可以參考下。

is_file() 函數檢查指定的檔案名稱是否為正常的檔案。

is_file — Tells whether the filename is a regular file

用法:
bool is_file ( string $filename ) $file 為必選參數
如果檔案存在且為正常的檔案則傳回TRUE。

先來看一個實例一:

<?php
var_dump(is_file(&#39;a_file.txt&#39;)) . "\n";
var_dump(is_file(&#39;/usr/bin/&#39;)) . "\n";
?>
登入後複製

上例將輸出:
bool(true)
bool(false)

實例二:

<?php
function isfile($file){
return preg_match(&#39;/^[^.^:^?^-][^:^?]*.(?i)&#39; . getexts() . &#39;$/&#39;,$file);
//first character cannot be . : ? - subsequent characters can&#39;t be a : ?
//then a . character and must end with one of your extentions
//getexts() can be replaced with your extentions pattern
}
function getexts(){
//list acceptable file extensions here
return &#39;(app|avi|doc|docx|exe|ico|mid|midi|mov|mp3|
mpg|mpeg|pdf|psd|qt|ra|ram|rm|rtf|txt|wav|word|xls)&#39;;
}
echo isfile(&#39;/Users/YourUserName/Sites/index.html&#39;);
?>
登入後複製

實例三:

<?php
function deletefolder($path)
{
if ($handle=opendir($path))
{
while (false!==($file=readdir($handle)))
{
if ($file<>"." AND $file<>"..")
{
if (is_file($path.&#39;/&#39;.$file))
{
@unlink($path.&#39;/&#39;.$file);
}
if (is_dir($path.&#39;/&#39;.$file))
{
deletefolder($path.&#39;/&#39;.$file);
@rmdir($path.&#39;/&#39;.$file);
}
}
}
}
}
?>
登入後複製

此函數將刪除所有檔案與資料夾。

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php針對目錄與檔案名稱的遞歸操作的方法

php中文字體及字串操作實作中文驗證碼

php結合正規則取得字串中數字的幾種方法

以上是PHP中is_file()函數的定義與使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!