php從文件夾隨機(jī)讀取文件的方法
更新時(shí)間:2015年06月01日 15:26:39 作者:不吃皮蛋
這篇文章主要介紹了php從文件夾隨機(jī)讀取文件的方法,可實(shí)現(xiàn)php從指定的目錄隨機(jī)讀取文件及設(shè)置參數(shù)進(jìn)行文件過濾的功能,需要的朋友可以參考下
本文實(shí)例講述了php從文件夾隨機(jī)讀取文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
function RandomFile($folder='', $extensions='.*'){
// fix path:
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;
// check folder:
if (!is_dir($folder)){ die('invalid folder given!'); }
// create files array
$files = array();
// open directory
if ($dir = @opendir($folder)){
// go trough all files:
while($file = readdir($dir)){
if (!preg_match('/^\.+$/', $file) and
preg_match('/\.('.$extensions.')$/', $file)){
// feed the array:
$files[] = $file;
}
}
// close directory
closedir($dir);
}
else {
die('Could not open the folder "'.$folder.'"');
}
if (count($files) == 0){
die('No files where found :-(');
}
// seed random function:
mt_srand((double)microtime()*1000000);
// get an random index:
$rand = mt_rand(0, count($files)-1);
// check again:
if (!isset($files[$rand])){
die('Array index was not found! very strange!');
}
// return the random file:
return $folder . $files[$rand];
}
//用法演示:
// "jpg|png|gif" matches all files with these extensions
print RandomFile('test_images/','jpg|png|gif');
// returns test_07.gif
// ".*" matches all extensions (all files)
print RandomFile('test_files/','.*');
// returns foobar_1.zip
// "[0-9]+" matches all extensions that just
// contain numbers (like backup.1, backup.2)
print RandomFile('test_files/','[0-9]+');
// returns backup.7
希望本文所述對大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- php遍歷、讀取文件夾中圖片并分頁顯示圖片的方法
- PHP讀取文件的常見幾種方法
- php fread讀取文件注意事項(xiàng)
- thinkPHP+PHPExcel實(shí)現(xiàn)讀取文件日期的方法(含時(shí)分秒)
- PHP中讀取文件的幾個(gè)方法總結(jié)(推薦)
- php文件操作小結(jié)(刪除指定文件/獲取文件夾下的文件名/讀取文件夾下圖片名)
- PHP使用fopen與file_get_contents讀取文件實(shí)例分享
- PHP讀取文件內(nèi)容的五種方式
- php讀取文件內(nèi)容到數(shù)組的方法
- PHP中讀取文件的8種方法和代碼實(shí)例
- PHP按行讀取文件時(shí)刪除換行符的3種方法
- php讀取文件內(nèi)容的幾種方法詳解
- PHP讀取文件并可支持遠(yuǎn)程文件的代碼分享
- php與c 實(shí)現(xiàn)按行讀取文件實(shí)例代碼
相關(guān)文章
PHP二維索引數(shù)組的遍歷實(shí)例分析【2種方式】
這篇文章主要介紹了PHP二維索引數(shù)組的遍歷,結(jié)合實(shí)例形式分析了php使用for循環(huán)與foreach循環(huán)2種方式遍歷數(shù)組的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
PHP使用preg_split和explode分割textarea存放內(nèi)容的方法分析
這篇文章主要介紹了PHP使用preg_split和explode分割textarea存放內(nèi)容的方法,結(jié)合實(shí)例形式分析preg_split和explode函數(shù)的功能、使用技巧與文本字符串分割過程中的相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
php提交post數(shù)組參數(shù)實(shí)例分析
這篇文章主要介紹了php提交post數(shù)組參數(shù)的用法,結(jié)合實(shí)例分析了php使用post進(jìn)行參數(shù)提交的相關(guān)技巧,需要的朋友可以參考下2015-12-12
PHP實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的類庫用法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的類庫用法,結(jié)合實(shí)例形式分析了php操作Excel數(shù)據(jù)的讀取與導(dǎo)出操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
php實(shí)現(xiàn)jQuery擴(kuò)展函數(shù)
今天在運(yùn)行書上的jQuery代碼時(shí),不知道是書上弄錯(cuò)了,還是我的jQuery版本的問題,例子上面有一個(gè)jQuery函數(shù)不存在。2009-10-10
php實(shí)現(xiàn)異步數(shù)據(jù)調(diào)用的方法
這篇文章主要介紹了php實(shí)現(xiàn)異步數(shù)據(jù)調(diào)用的方法,分享了4種PHP異步執(zhí)行的常用方式,感興趣的小伙伴們可以參考一下2015-12-12

