Joomla 文件夾操作

2023-03-10 15:55 更新

Joomla的文件系統(tǒng)提供了對(duì)常規(guī)文件操作的封裝,主要由4個(gè)類 JFile,JFolder,JPath,JArchive來實(shí)現(xiàn)。本文主要講解JFolder類的一些常用文件夾操作 

復(fù)制文件夾

代碼:

JFolder::copy($src, $dest, $path, $force);

這個(gè)函數(shù)會(huì)將$src指定的文件夾及其所有內(nèi)容(包括子目錄)復(fù)制到$dest指定的位置。它會(huì)檢查原地址和目標(biāo)地址是否存在且權(quán)限是否允許。這個(gè)方法支持FTP層操作

  • 如果目標(biāo)路徑不存在,則會(huì)嘗試創(chuàng)建。
  • $path指定相對(duì)路徑
  • $firce是否覆蓋目標(biāo)文件夾。如果目標(biāo)路徑存在,且$force為false的時(shí)候,會(huì)提示錯(cuò)誤。

創(chuàng)建文件夾

代碼:

JFolder::create($path, $mode);

 這個(gè)方法是對(duì)PHP的mkdir()方法進(jìn)行封裝,在此基礎(chǔ)上增加了權(quán)限檢查和路徑可用性檢查。這個(gè)方法支持FTP層操作。

  • $mode參數(shù)可以指定創(chuàng)建文件夾的權(quán)限。默認(rèn)情況下成功創(chuàng)建文件夾后權(quán)限會(huì)設(shè)置為0755
  • 如果父路徑不存在,則會(huì)嘗試創(chuàng)建。如果目錄已經(jīng)存在,則會(huì)返回false

移動(dòng)文件夾

代碼:

JFolder::move($src, $dest);

這個(gè)方法是對(duì)PHP的rename()方法進(jìn)行封裝,在此基礎(chǔ)上增加了權(quán)限檢查和路徑可用性檢查。這個(gè)方法支持FTP層操作。

檢查文件夾是否存在

代碼:

JFolder::exists($path);

這個(gè)函數(shù)的實(shí)現(xiàn)是對(duì)PHP 的is_dir() 函數(shù)的封裝,如果存在返回true

清理文件夾路徑

代碼:

JFolder::makeSafe($path);

這個(gè)函數(shù)會(huì)過濾掉文件名中不合法的字符(odd characters),返回一個(gè)安全的文件夾名

讀取文件夾中的所有文件

代碼:

JFolder::files($path, $filter = '.', $recurse, $fullpath , $exclude);

這個(gè)函數(shù)讀取指定文件夾中的所有文件。

  • $path 指定需要讀取文件夾的路徑
  • $filter 過濾器
  • $recurse 是否遞歸
  • $fullpath 是否返回全路徑
  • $exclude  數(shù)組 需要排除的文件擴(kuò)展名。實(shí)際使用中感覺這個(gè)參數(shù)無效

讀取文件夾中的所有文件夾

代碼:

JFolder::folders($path, $filter = '.', $recurse, $fullpath , $exclude);

這個(gè)函數(shù)讀取指定文件夾中的所有文件夾。使用方法和JFolder::files一樣

得到文件夾數(shù)結(jié)構(gòu)

JFolder::listFolderTree($path, $filter, $maxLevel = 3, $level = 0, $parent = 0);

它將讀取在$path指定的文件夾,并將結(jié)果以數(shù)組的方法返回,適合于樹形顯示。您可以指定級(jí)別數(shù)。文件夾數(shù)組如下所示

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [name] => administrator
            [fullname] => g:/joomla_1012/administrator
            [relname] => g:/joomla_1012/administrator
        )
 
    [1] => Array
        (
            [id] => 2
            [parent] => 1
            [name] => backups
            [fullname] => g:/joomla_1012/administrator/backups
            [relname] => g:/joomla_1012/administrator/backups
        )
 
    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [name] => components
            [fullname] => g:/joomla_1012/administrator/components
            [relname] => g:/joomla_1012/administrator/components
        )
)

示范代碼

本代碼演示了如何使用文件系統(tǒng)來做復(fù)制移動(dòng)操作

目標(biāo):

讀取根目錄下images文件夾的內(nèi)容。創(chuàng)建一個(gè)名為jpg的子文件夾,然后images中所有jpg文件移至jpg子文件夾。

實(shí)現(xiàn)代碼:

<?php
// First we set up parameters.
$searchpath = JPATH_COMPONENT . '/images';
 
// Import the folder system library.
jimport('joomla.filesystem.folder');
 
// Then we create the subfolder called jpg.
if (!JFolder::create($searchpath . "/jpg"))
{
   // Throw error message and stop script.
}
 
// Now we read all jpg files and put them in an array.
$jpgFiles = JFolder::files($searchpath, '.jpg');
 
// Now we need some stuff from the ''JFile:: class'' to move all the files into the new folder.
foreach ($jpgFiles as $file)
{
   JFile::move($searchpath . '/' . $file, $searchpath . '/' . 'jpg' . $file);
}
 
// Last we move the complete subdir to the root of the component.
if (JFolder::move($searchpath . '/'. 'jpg', JPATH_COMPONENT))
{
   // Redirect with perhaps a happy message.
}
else
{
   // Throw an error.
}
?>

 


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)