2012-03-30 44 views
2

我嘗試做這個文件名:如何讓PHP發現含有一種

$filename = "/destination/destination2/file_*"; 

「*」=什麼

DESTINATION2文件:

file_somethingrandom

而且由於它$ filename包含*所以它應該選擇file_somethingrandom

如何做到這一點?

+0

所以你想要從文件目錄中選擇一個隨機有效的現有文件? – 2012-03-30 12:50:18

+0

它其實不是隨機的,把我只是舉一個例子,我想它找到它包含的東西的文件名,如果它存在然後生病要求它死 – 2012-03-30 12:52:55

回答

8

使用​​3210功能是這樣的:

http://ca.php.net/glob

<?php 
$filename = "/destination/destination2/file_*"; 
foreach (glob($filename) as $filefound) { 
    echo "$filefound size " . filesize($filefound) . "\n"; 
} 
?> 
+0

如何排列$ filefound – 2012-03-30 13:37:28

+0

'$ filefound'不是一個數組。如果你想要一個數組,可以這樣想:'$ filesfound = glob($ filename);'。然後你將有一個名爲'$ filesfound'的數組,其中包含所有找到的文件。 – roychri 2012-03-30 14:08:57

2

試試這個

foreach (glob("/destination/destination2/file_*") as $filename) { 
    echo $filename; 
} 

乾杯!