php取子目錄
❶ php 讀取目錄和文件
如果不知道子目錄的命名和個數,則需要先遍歷子目錄
opendir,readdir,closedir等即可
或者使用類dir,具體參考參見PHP說明文檔
http://cn.php.net/manual/en/ref.dir.php
下面是一段示例代碼:
$dirhandle = opendir("../prodocs");
while(($file = readdir($dirhandle)) != NULL)
{
if(is_dir("../prodocs/$file"))
{
continue;
}
.........
}
closedir($dirhandle);
讀取文件,根據上面的遍歷結果和指定的文件名即可開始讀取文件內容
具體函數和C標准函數是類似的
fopen,fclose,fseek,fread,fwrite等
這是一段示例代碼(來自PHP文檔):
<?php
$handle = fopen("http://www.example.com/", "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
❷ php如何遍歷目錄及子目錄
<?php
/*
* @src始源文件的地址
* @dest是目標文件的地址
* @file_permission目標文件的許可權
*/
function file($src, $dest, $file_permission = 0644) {
$src = str_replace('\\', '/', $src);
$src = str_replace('//', '/', $src);
$dest = str_replace('\\', '/', $dest);
$dest = str_replace('//', '/', $dest);
if (is_file($src) ) { //只能進行文件的復制/如想復制文件夾,自己寫方法吧
if(is_dir($dest)) {
if ($dest[ strlen($dest)-1 ] != '/') {
$__dest = $dest . "/";
}
$__dest .= basename($src);
} else {
$__dest = $dest;
}
$res = ($src, $__dest);
chmod($__dest, $file_permission);
}
}
function procrss_($dir, $file) {
$_exclude = array('.', '..', '.svn');
$d = dir($dir);
while (false !== ($entry = $d->read())) {
file($file, $dir. '/' . $entry);
if(!in_array($entry, $_exclude)) {
if(is_dir($dir . '/'.$entry)) {
procrss_($dir . '/'.$entry); //遞歸讀文件
} else {
echo $entry . "\n";
}
}
}
$d->close();
}
procrss_($newdir, 'Wring.txt');
process_你總要調用執行復制的函數吧
❸ PHP如何獲取文件夾下所以子文件夾的名稱及子夾所以圖片
這個用到的技術就php對文件的操作,文件遍歷。
這里有個我自己寫文件夾遍歷函數,你看看測試下。
//遞歸遍歷文件夾,輸出所有文件的路徑
functionbianli($path){
static$arr=array();
$str="";
if($open=opendir($path)){
while($f=readdir($open)){
$str=$path;
if($f!='.'&&$f!='..'){
$str=$str."/".$f;
if(is_dir($str)){
$i++;
bianli($str);
}else{
$arr[]=$str;
//echo$str."<br>";
}
}
}
}
return$arr;
}
//遍歷文件夾
functionsearch_one_file($path){
$arr=array();
if($open=opendir($path)){
while($f=readdir($open)){
if($f!='.'&&$f!='..'){
$arr[]=$path."/".$f;
}
}
}
return$arr;
}
❹ wordpress PHP獲取指定父級目錄下子級目錄的名稱和URL
這個已內置了相關函數,具體用法:
$args=array(
'child_of'=>3,//獲取指定ID下的所有子級目錄
'show_count'=>1//顯示文章合計數
);
wp_list_categories($args);
建議參考
/wp-includes/ -> category-template.php ( 382 行起 )中的wp_list_categories函數的具體使用。
❺ 怎麼用PHP創建目錄和子目錄
<?php
header("Content-type:text/html;charset=utf-8");
//設置要創建的目錄(可設置多級)
$path="/";
//首先判斷目錄存在否
if(is_dir($path)){
echo"抱歉,目錄".$path."已存在!";
}else{
//第3個參數「true」意思是能創建多級目錄,iconv防止中文目錄亂碼
$res=mkdir(iconv("UTF-8","GBK",$path),0777,true);
if($res){
echo"$path創建成功";
}else{
echo"$path創建失敗";
}
}
?>
❻ php列出目錄下所有文件(包括子目錄)
<?php
/**
*Goofy2011-11-30
*getDir()去文件夾列表,getFile()去對應文件夾下面的文件列表,二者的區別在於判斷有沒有「.」後綴的文件,其他都一樣
*/
//獲取文件目錄列表,該方法返回數組
functiongetDir($dir){
$dirArray[]=NULL;
if(false!=($handle=opendir($dir))){
$i=0;
while(false!==($file=readdir($handle))){
//去掉"「.」、「..」以及帶「.xxx」後綴的文件
if($file!="."&&$file!=".."&&!strpos($file,".")){
$dirArray[$i]=$file;
$i++;
}
}
//關閉句柄
closedir($handle);
}
return$dirArray;
}
//獲取文件列表
functiongetFile($dir){
$fileArray[]=NULL;
if(false!=($handle=opendir($dir))){
$i=0;
while(false!==($file=readdir($handle))){
//去掉"「.」、「..」以及帶「.xxx」後綴的文件
if($file!="."&&$file!=".."&&strpos($file,".")){
$fileArray[$i]="./imageroot/current/".$file;
if($i==100){
break;
}
$i++;
}
}
//關閉句柄
closedir($handle);
}
return$fileArray;
}
//調用方法getDir("./dir")……
?>
❼ php讀取目錄及子目錄下所有txt文件,再替換每個txt文件里system字元
php中讀取目錄下的文件名的方式確實不少,最簡單的是scandir,具體代碼如下:
$dir="./目錄名/";
$file=scandir($dir);
print_r
($file);
❽ PHP 如何取得子目錄 session
想要共享 SESSION 數據,那就必須實現兩個目標:一個是各個伺服器對同一個客戶端產生的 SESSION ID 必須相同,並且可通過同一個 COOKIE 進行傳遞,也就是說各個伺服器必須可以讀取同一個名為 PHPSESSID 的 COOKIE;另一個是 SESSION 數據的存儲方式/位置必須保證各個伺服器都能夠訪問到。
第一個目標的實現其實很簡單,只需要對 COOKIE 的域(domain)進行特殊地設置即可
第二個目標由於你在同一個伺服器下面,所以不需要設置
❾ php包含子目錄文件中的路徑問題
在index.php文件里,把他所在的絕對路徑獲取到,是文件夾目錄路徑,如D:/www,把這個值define一個常量,然後以後再include什麼文件,都用剛才的常量加目錄加文件名字.
這還有幾個關於包含路徑的總結文章,可以看下.
http://www.xphper.com/index.php?load=read&id=102
http://www.xphper.com/index.php?load=read&id=761
❿ php讀取目錄及子目錄下所有txt文件,再替換每個txt文件里指定字元
直接給你代碼:
<?php
functionreplaceStringInTxt($path)
{
$path=ltrim($path,'/').'/';
$files=scandir($path);
if($files){
foreach($filesas$file){
if($file=='.'||$file=='..'){
continue;
}
$newPath=$path.$file;
if(is_dir($newPath){
replaceStringInTxt($newPath);
}else{
//開始替換
$content=file_get_contents($newPath);
$content=str_replace("system****config","config",$content);
file_put_contents($newPath,$content);
}
}
}
}
//目錄的地址
$path="C://aa";
//執行
replaceStringInTxt($path);
By the way, 代碼未經檢驗,請自行 debug , 隨便寫的。