-
完結(jié)!
【關(guān)于老師課程中目錄讀取的函數(shù)封裝】
我寫了兩個(gè)版本的目錄讀取
第一種是老師的寫法
第二種是我在網(wǎng)上搜索問題時(shí)無意發(fā)現(xiàn)的方法
<?php //第一種 //使用opendir()、readdir()、closedir()遍歷給定目錄下的所有文件 function?readDirectory($path){ ????if?(!is_dir($path)){ ????????return?'不是一個(gè)目錄'; ????} ????$handle?=?opendir($path); ????$arr?=?[]; ????while?(($file?=?readdir($handle))?!==?false){ ????????if?($file?!=?'.'?&&?$file?!=?'..'){ ????????????if?(is_file($path?.?'/'?.?$file)){ ????????????????$arr['file'][]?=?$file; ????????????} ????????????if?(is_dir($path?.?'/'?.?$file)){ ????????????????$arr['dir'][]?=?$file; ????????????} ????????} ????} ????closedir($handle); ????return?$arr; } //第二種 //使用scandir直接掃描指定目錄下的所有文件 function?scanDirectory($path){ ????$arr?=?@scandir($path,SCANDIR_SORT_NONE); ????$arr2?=?[]; ????if?(!$arr){ ????????return?'不是一個(gè)目錄'; ????} ????foreach?($arr?as?$item){ ????????if?($item?!=?'.'?&&?$item?!=?'..'){ ????????????$file_path?=?$path?.?'/'?.?$item; ????????????if?(is_dir($file_path)){ ????????????????$arr2['dir'][]?=?$item; ????????????} ????????????if?(is_file($file_path)){ ????????????????$arr2['file'][]?=?$item; ????????????} ????????} ????} ????return?$arr2; } //以上兩種方法完全一致,只是scandir方法的第二個(gè)參數(shù)可以按照字母的升降序排列 //上傳文件 function?uploadFile($filename,$path){ ????if?(!$filename['error']){ ????????if?(is_uploaded_file($filename['tmp_name'])){ ????????????if?(move_uploaded_file($filename['tmp_name'],$path?.?'/'?.?$filename['name'])){ ????????????????return?'上傳成功'; ????????????}else{ ????????????????die(); ????????????????return?'上傳失敗'; ????????????} ????????}else{ ????????????return?'不是通過HTTP?POST上傳的,請(qǐng)重試!'; ????????} ????}else{ ????????return?'上傳出錯(cuò),錯(cuò)誤號(hào):'?.?$filename['error']; ????} }
我覺得還是scandir方法好用,簡單,方便,快捷;
查看全部 -
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html?xmlns="http://www.w3.org/1999/xhtml"> <head> ????<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"?/> ????<title>Insert?title?here</title> ????<link?rel="stylesheet"?href="cikonss.css"?/> ????<style?type="text/css"> ????????body,p,div,ul,ol,table,dl,dd,dt{ ????????????margin:0; ????????????padding:?0; ????????} ????????a{ ????????????text-decoration:?none; ????????} ????????ul,li{ ????????????list-style:?none; ????????????float:?left; ????????} ????????#top{ ????????????width:100%; ????????????height:48px; ????????????margin:0?auto; ????????????background:?#E2E2E2; ????????} ????????#navi?a{ ????????????display:?block; ????????????width:48px; ????????????height:?48px; ????????} ????????#main{ ????????????margin:0?auto; ????????????border:2px?solid?#ABCDEF; ????????} ????????.small{ ????????????width:25px; ????????????height:25px; ????????????border:0; ????????} ????</style> </head> <body> <h1>慕課網(wǎng)-在線文件管理器</h1> <div?id="top"> ????<ul?id="navi"> ????????<li><a?href="index.php"?title="主目錄"><span??class="icon?icon-small?icon-square"><span?class="icon-home"></span></span></a></li> ????????<li><a?href="#"??onclick="show('createFile')"?title="新建文件"?><span??class="icon?icon-small?icon-square"><span?class="icon-file"></span></span></a></li> ????????<li><a?href="#"??onclick="show('createFolder')"?title="新建文件夾"><span??class="icon?icon-small?icon-square"><span?class="icon-folder"></span></span></a></li> ????????<li><a?href="#"?onclick="show('uploadFile')"title="上傳文件"><span??class="icon?icon-small?icon-square"><span?class="icon-upload"></span></span></a></li> ????????<li><a?href="#"?title="返回上級(jí)目錄"?onclick="goBack('<?php?echo?$back;?>')"><span??class="icon?icon-small?icon-square"><span?class="icon-arrowLeft"></span></span></a></li> ????</ul> </div> </body> </html>
剝出來的靜態(tài)網(wǎng)頁
查看全部 -
1查看全部
-
最后效果的 操作界面
查看全部 -
文件夾的相關(guān)操作
查看全部 -
文件相關(guān)操作
查看全部 -
就是通過瀏覽器,對(duì)服務(wù)器項(xiàng)目實(shí)現(xiàn)遠(yuǎn)程操作
查看全部 -
刪除文件夾查看全部
-
通過header()函數(shù)發(fā)送網(wǎng)頁頭信息來實(shí)現(xiàn)文件下載查看全部
-
$arr=explode('.',$val); //根據(jù)符號(hào)‘.’將文件名分割 $ext=strtolower(end($arr)); //取得文件名被分割后的最后那一部分,即擴(kuò)展名查看全部
-
的三個(gè)地方給老夫查看全部
-
打開文件下的目錄或者內(nèi)容是 opendir()函數(shù);返回的是array數(shù)組查看全部
-
需要完成的文件相關(guān)操作查看全部
-
filetype() :返回文件類型 filesize():返回文件大小(字節(jié)) round():四舍五入,第二位參數(shù)是保留小數(shù)查看全部
-
is_file()判斷是否為文件 is_dir()判斷是否為目錄 closedir()關(guān)閉文件讀取 .表示當(dāng)前目錄, ..表示上級(jí)目錄 $handle = openddir($path):打開目錄 readdir($handle);:讀取目錄查看全部
舉報(bào)
0/150
提交
取消