php中開啟目錄並輸出目錄檔案實現代碼

來源:互聯網
上載者:User

 

文法
opendir(path,context)
*/
$d=dir("c:windows");         //開啟目錄,並賦值給變數
echo "handle:".$d->handle."n";       //輸出目錄的handel屬性
echo "<p>";
echo "path:".$d->path."n";        //輸出目錄的path屬性
echo "<p>";
while(false!==($entry=$d->read()))       //通過迴圈讀取對象的方法
{
  echo $entry."n";
}
$d->close();           //關閉目錄控制代碼


//


$dir="c:windows";          //定義目錄
if(is_dir($dir))           //如果變數為目錄
{
  if($dh=opendir($dir));         //如果成功開啟目錄
  {
    echo "目錄".$dir."成功開啟";
  }
}
echo "<br>";
closedir($dh);          //關閉開啟的目錄控制代碼
echo "目錄".$dir."已經關閉!";


///

$dir=" c:windows ";          //定義目錄變數
if(is_dir($dir))           //判斷變數是否為目錄
{
  if($dh=opendir($dir))         //判斷目錄是否成功開啟
  {
    while(($file=readdir($dh))!==false)      //迴圈讀取目錄中的檔案資訊
    {
      echo "filename:$file,filetype:".filetype($dir.$file)."n";  //輸出檔案名及檔案類型
    }
    closedir($dh);          //關閉目錄控制代碼
  }
}

///

if($handle=opendir('web'))         //如果目錄順利開啟
{
  while(false!==($file=readdir($handle)))      //迴圈讀取目錄中的條目
  {
    if($file!="."&&$file!="..")        //如果檔案不為當前或上級目錄
    {
      echo "$filen";          //輸出檔案名稱
    }
  }
  closedir($handle);          //關閉目錄控制代碼
}

//

if($handle=opendir('08'))         //如果目錄順利開啟
{
  while(false!==($file=readdir($handle)))      //迴圈讀取目錄中的條目
  {
    if($file!="."&&$file!="..")        //如果檔案不為當前或上級目錄
    {
      echo "$filen";          //輸出檔案名稱
    }
  }
  rewind($handle);          //倒回目錄控制代碼
  if(readdir($handle))       //判斷是否倒回成功
  {
    echo "倒回成功";
  }
  else
  {
    echo "倒回失敗";
  }
  closedir($handle);          //關閉目錄控制代碼
}

//如果要對目錄按字母排序如一

$files1=scandir($dir);         //按字母升序列出內容到數組
$files2=scandir($dir,1);        //按字母降序列出內容到數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.