2017-08-09 54 views
0

我試圖從一個文件夾中獲取svg文件。php獲取內容圖像文件夾svg

嘗試下面的方法,但沒有人似乎工作:

<?php 
$directory = get_bloginfo('template_directory').'/images/myImages/';  
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); 
while ($it->valid()) { //Check the file exist 
    if (!$it->isDot()) { //if not parent ".." or current "." 
     if (strpos($it->key(), '.php') !== false 
      || strpos($it->key(), '.css') !== false 
      || strpos($it->key(), '.js') !== false 
     ) { 
     echo $it->key() . '<br>'; 
     } 
    } 
} 
?> 

和:

global $wp_filesystem; 
$path = get_bloginfo('template_directory').'/images/myImages/'; 
$filelist = $wp_filesystem->dirlist($path); 
echo $filelist; 

和:

$path = get_bloginfo('template_directory').'/images/myImages/'; 
$images = scandir($path, 'svg', $depth = 0); 
echo $images; 

和:

$dir = get_bloginfo('template_directory').'/images/myImages/'; 
$files = scandir($dir); 
print_r($files); 

和:

$directory = get_bloginfo('template_directory')."/images/myImages/"; 
$images = glob($directory . "*.svg"); 
echo '<pre>'; 
print_r($images); 
echo '</pre>'; 
echo $directory.'abnamro.svg">'; 
foreach($images as $image) 
    { 
     echo $image; 
    } 

我有點迷失。我可能認爲還有其他的錯誤。
還檢查了用戶的權限,但一切正常。 我用MAMP在本地機器上運行Wordpress。

有什麼想法?

+0

您是否嘗試過使用SCANDIR得到的文件夾中的文件列表和循環他們? http://php.net/manual/en/function.scandir.php – raphael75

+0

還沒有打圈呢。我試了一下,並檢查了數組中的值,但數組是空的。不會得到任何錯誤 – Interactive

+1

它可能是在錯誤的文件夾中查找? get_bloginfo('template_directory')是否返回正確的文件夾? – raphael75

回答

2

請嘗試下面的功能,爲了清楚起見,我已經註明。一些亮點有:

  1. 可以跳過一開始就點目錄中的迭代器
  2. 如果路徑不存在(在這種情況下,問題可以觸發一個致命的錯誤,您正在使用域-root路徑,而不是服務器的根路徑[ABSPATH])
  3. 您可以選擇擴展類型來過濾文件

function getPathsByKind($path,$ext,$err_type = false) 
    { 
     # Assign the error type, default is fatal error 
     if($err_type === false) 
      $err_type = E_USER_ERROR; 
     # Check if the path is valid 
     if(!is_dir($path)) { 
      # Throw fatal error if folder doesn't exist 
      trigger_error('Folder does not exist. No file paths can be returned.',$err_type); 
      # Return false incase user error is just notice... 
      return false; 
     } 
     # Set a storage array 
     $file = array(); 
     # Get path list of files 
     $it  = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::SKIP_DOTS) 
     ); 
     # Loop and assign paths 
     foreach($it as $filename => $val) { 
      if(strtolower(pathinfo($filename,PATHINFO_EXTENSION)) == strtolower($ext)) { 
       $file[] = $filename; 
      } 
     } 
     # Return the path list 
     return $file; 
    } 

要使用:

# Assign directory path 
$directory = str_replace('//','/',ABSPATH.'/'.get_bloginfo('template_directory').'/images/myImages/'); 
# Get files 
$files = getPathsByKind($directory,'svg'); 
# Check there are files 
if(!empty($files)) { 
    print_r($files); 
} 

如果路徑不存在,它會由現在的系統錯誤的方式,路徑不存在告訴你。如果它不會拋出致命的錯誤並空出來,那麼你確實會遇到一些奇怪的問題。

如果一切順利的話,你應該得到的東西,如:

Array 
(
    [0] => /data/19/2/133/150/3412/user/12321/htdocs/domain/images/myImages/img1.svg 
    [1] => /data/19/2/133/150/3412/user/12321/htdocs/domain/images/myImages/img2.svg 
    [2] => /data/19/2/133/150/3412/user/12321/htdocs/domain/images/myImages/img3.svg 
    [3] => /data/19/2/133/150/3412/user/12321/htdocs/domain/images/myImages/img4.svg 
) 

如果路徑無效,將拋出:

Fatal error: Folder does not exist. No file paths can be returned. in /data/19/2/133/150/3412/user/12321/htdocs/domain/index.php on line 123

+0

謝謝你的解釋。它確實給我一個錯誤。說這個文件夾不存在。唯一的問題是該文件夾確實存在。權限是正確的......我試圖使用絕對路徑,但它一直給我錯誤。這可能是MAMP /本地問題嗎?我會把它放在網上,看看會發生什麼 – Interactive

+0

只是要確定.....我將函數添加到我的'functions.php' ....沒有工作,雖然 – Interactive

+0

我echo'd目錄並粘貼它直接進入我的瀏覽器。它給了我一個該文件夾內所有項目的清單....我迷失在這裏。 – Interactive