導航:首頁 > 編程語言 > php檢測文件存在

php檢測文件存在

發布時間:2022-06-15 11:45:58

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 檢測文件是否存在的幾種方式

一、 file_exists();

二、is_file();

$file='test';

file_exists($file)ORexit('該目錄不存在');

is_file($file)ORexit('該目錄不存在');

file_exists 既可以用來檢查文件夾,也可以用來檢查文件

is_file 只能用來檢查文件

❸ php如何檢測文件內是否存在特定內容

一般使用curl獲取,以前可以用file_get_contents獲取
<?php
$res = file_get_contents('url地址或文件'); // 得到結果
if ($res != 'not') {
// 彈窗需要使用js

echo "<script>alert('輸出信息');window.location='需要跳轉的地址';</script>";

}

❹ php判斷文件是否存在

通過代碼查看,這段代碼是js代碼非PHP代碼,是使用jquery寫的,那麼判斷當前圖片載入失敗或不存在時更換圖片的代碼如下:

$('img').error(function(){
$(this).attr('src','載入失敗.png');
});

❺ 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判斷文件夾是否存在不存在則創建

if(file_exists($file)) 存在;
else 不存在;
if(is_dir($dir)) 存在;
else 不存在;
mkdir($dir); //創建文件夾
file_put_contents('文件路徑', '文件內容');//創建文件

❼ PHP用什麼方法判斷一個文件存在

在PHP中,使用file_exists()函數可以判斷一個給定的文件是否存在。如下的程序例子給出了判斷「logfile.txt「是否存在的例子。

<?php
$filename = "logfile.txt";
if ( file_exists( $logfile ) ){
echo "file ".filename." was found";
}
?>

❽ 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

❾ 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怎麼判斷是否有某個文件夾

php 語言提供了 file_exists 函數,其功能是:


file_exists—檢查文件或目錄是否存在


函數原型定義如下:

boolfile_exists(string$filename)


示例代碼:

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

與php檢測文件存在相關的資料

熱點內容
證據提取命令視頻 瀏覽:353
java的學習心得 瀏覽:96
prof命令 瀏覽:279
手機加密文件密碼怎麼解開 瀏覽:283
賈躍亭程序員完整視頻 瀏覽:958
怎樣把兩個文件夾打包發送 瀏覽:378
單片機教程資料 瀏覽:982
仿大眾點評系統源碼python 瀏覽:426
手機網路伺服器連接不上是怎麼回事 瀏覽:155
電腦為什麼一直要解壓 瀏覽:530
淘客優惠券網站源碼 瀏覽:555
word轉成pdf在線 瀏覽:775
手機暴力解壓教程 瀏覽:130
解壓小視頻第二期 瀏覽:364
裝機自帶軟體找不到軟體文件夾 瀏覽:330
仙境之路伺服器地址ip 瀏覽:708
華為服務app是什麼東西 瀏覽:180
關於單片機的視頻 瀏覽:592
淘寶直播app緩存怎麼清理 瀏覽:555
android可以刷機嗎 瀏覽:350