文、意如
1.顯示檔案名稱
2.分割網頁路徑
3.抓取絕對路徑
4.掃描目錄中的資料夾
5.刪除檔案
1.顯示檔案名稱
<?php
$path = $_SERVER['PHP_SELF'];
echo $path.'<br>'; #/yiru/file/index.php
echo '目前的檔案名稱:'.basename($path).'<br>'; #index.php
echo '顯示目前網頁的檔案名稱,但排除副檔名'.basename($path,'.php').'<br>'; #index
?>

2.分割網頁路徑
<?php
$path = $_SERVER['PHP_SELF'];
//echo $path."<br>";
$myinfo = pathinfo($path);
echo '目前網頁的路徑:'.$path.'<BR>'; # /yiru/file/index.php
echo '分割出來的路徑名稱:'.$myinfo['dirname'].'<br>'; # /yiru/file
echo '分割出來的檔案名稱:'.$myinfo['basename'].'<br>';#index.php
echo '抓取檔案名:'.$myinfo['filename'].'<br>'; #index
echo '分割出來的副檔名:'.$myinfo['extension'].'<br>'; # php
?>

3.抓取絕對路徑
<?php
$path=basename($_SERVER['PHP_SELF']);
echo $path.'<br>'; #index.php
echo '絕對路徑:'.realpath($path);#C:\xampp\htdocs\yiru\file\index.php
?>

4. 掃描目錄中的資料夾
<?php
$file=scandir("C:\Yiru");
var_dump($file); #array(6) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(4) "Book" [3]=> string(4) "Code" [4]=> string(4) "Self" [5]=> string(12) "YiruAtStudio" }
echo "<br>";
print_r($file);#Array ( [0] => . [1] => .. [2] => Book [3] => Code [4] => Self [5] => YiruAtStudio )
echo "<br>";
foreach($file as $value){
if ($value != "." && $value != "..") {
echo $value . "<br>";
#Book
#Code
#Self
#YiruAtStudio
}
}
?>

5.刪除檔案
<?php
$filename = "mytest.txt";
#echo file_exists($filename); #判斷檔案是否存在,存在回傳1
if(file_exists($filename)){ #當檔案存在時(1=true)
unlink($filename); #刪除檔案
echo "刪除檔案成功!";
}else{
echo "檔案不存在,刪除檔案失敗!";
}
?>


Yiru@Studio - 關於我 - 意如