搜尋
首頁後端開發php教程PHP檔案與目錄操作

PHP檔案與目錄操作

May 29, 2018 am 10:49 AM
php操作目錄

這篇文章主要介紹了PHP檔案與目錄操作,涉及php針對檔案與目錄的遍歷、判斷與排序相關操作技巧,註解中備有較為詳細的說明,需要的朋友可以參考下

本文實例敘述了PHP檔案與目錄操作。分享給大家供大家參考,具體如下:

檔案目錄相關函數

<?php
// 输出目录中的文件
function outputcurfiles ($allowedtypes, $thedir){
//首先,我们确保目录存在。
if (is_dir ($thedir)){
 //现在,我们使用scandir扫描目录中的文件。
 $scanarray = scandir ($thedir);
 //接着我们开始解析数组。
 //scandir()用“.”和“..”统计文件导航列表
 //因此作为文件,我们不应该列出他们。
 for ($i = 0; $i < count ($scanarray); $i++){
  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
   //现在,进行检查,以确保这是一个文件,而不是一个目录。
   if (is_file ($thedir . "/" . $scanarray[$i])){
    //现在,因为我们将允许客户端编辑这个文件,
    //我们必须检查它是否是可读和可写。
    if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){
     //现在,我们检查文件类型是否存在于允许的类型数组中.
     $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
     if (in_array ($thepath[&#39;extension&#39;], $allowedtypes)){
      //如果文件符合规定,我们可以继续输出.
      echo $scanarray[$i] . "<br />";
     }
    }
   }
  }
 }
} else {
 echo "对不起,这个目录不存在.";
}
}
$allowedtypes = array ("txt","html");
outputcurfiles ($allowedtypes, "testfolder");
///////////////////////////////////////////////////
function recurdir ($thedir) {
  //First attempt to open the directory.
  try {
    if ($adir = opendir ($thedir)){
      //扫描目录。
      while (false !== ($anitem = readdir ($adir))){
        //不统计目录中包含“.”或“..”的情况
        if ($anitem != "." && $anitem != ".."){
          //此时如果是一个目录,则缩进一点
          //再去递归
          if (is_dir ($thedir . "/" . $anitem)){
            ?><span style="font-weight: bold;" mce_style="font-weight: bold;"><?php echo $anitem; ?></span><?php
            ?><p style="margin-left: 10px;" mce_style="margin-left:10px;"><?php
            recurdir ($thedir . "/" . $anitem );
            ?></p><?php
          } elseif (is_file ($thedir . "/" . $anitem)){
            //此时输出文件.
            echo $anitem . "<br />";
          }
        }
      }
    } else {
      throw new exception ("Sorry, directory could not be openend.");
    }
  } catch (exception $e) {
    echo $e->getmessage();
  }
}
echo "<br />/////////////////////////////////////<br /><br />";
recurdir("testfolder");
//////////////////////////////////////////////////////////////////
echo "<br />/////////////////////////////////////<br /><br />";
function sortfilesbydate ($thedir){
  //首先,需要确保目录存在。
  if (is_dir ($thedir)){
    //接着,我们使用scandir扫描此目录中的文件.
    $scanarray = scandir ($thedir);
    $finalarray = array();
    //然后开始解析数组
    //scandir()用“.”和“..”统计文件导航列表
    //因此作为文件,我们不应该列出他们.
    for ($i = 0; $i < count ($scanarray); $i++){
      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
        //现在,我们检查,以确保这是一个文件,而不是一个目录.
        if (is_file ($thedir . "/" . $scanarray[$i])){
          //现在需要做的是循环数据到一个关联数组.
          $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
        }
      }
    }
    //至此,我们已经遍历了整个数组,现在需要做的只是asort()它。
    asort ($finalarray);
    return ($finalarray);
  } else {
    echo "对不起,这个目录不存在.";
  }
}
//然后,我们将函数指向我们需要查看的目录.
$sortedarray = sortfilesbydate ("testfolder");
//至此,就可以按照如下形式输出:
while ($element = each ($sortedarray)){
  echo "File: " . $element[&#39;key&#39;] . " was last modified: " . date ("F j, Y h:i:s", $element[&#39;value&#39;]) . "<br />";
}
?>

##以上就是本文的全部內容,希望對大家的學習有幫助。


相關推薦:

php檔案目錄作業的開發過程與範例分享

#php如何遍歷檔案目錄的方法教學

#php chmod()函數與批次修改檔案目錄權限

#

以上是PHP檔案與目錄操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器