QnA. 유동적인 디렉토리명
[ QNA / QNA-Change Dir Name / Leave Comment ]
b.php 파일은 a.php 파일을 인클루드해서 정보를 가져오는 내용이 유동적인 파일입니다.
a.php 파일에서 b.php파일을 불러오는 펑션은 다음과 같습니다.
<?php
.
.
function get_bfile() {
$폴더명 = '유동적인 폴더';
echo "/".$폴더명."/b.php";
}
.
.
?>
c.php 을 보면 <a href="폴더명/b.php">download</a>
이런식으로 표현되기 위해서
c.php 파일에 <a href="<?php get_bfile(); ?>" >download</a>
이런식으로 삽입합니다.
Q원래는 유동적인 폴더가 아닌데 하루에 1번씩 폴더 이름이 바뀌었으면 좋겠어서요.. 어떻게 해야 하나요?
Kebie님이 도와주심 - 감사합니다.!
- 아래의 소스에서
$root_path, $web_path를 수정해주세요. -
/wp-content/plugins/하위에 이름이dynamic_dir_로 시작하는 디렉토리를 하나 만들어 두세요. (최초 한번만 하면 됨.) 방금만든 디렉토리의 권한을 777로 변경해주세요. (최초한번만 하면 됨)
이렇게 하고 아래의 코드를 a.php 에 삽입하시면 앞으로 a.php에 접근할 때마다, 폴더명이 바뀝니다.
function get_bfile(){
$root_path = "/home/kebie/wp"; // 워드프레스가 설치된 절대경로명
$web_path = "/wp-content/plugins/"; // 워드프레스 하위경로명 반드시 / 로 끝나는 경로명
$path = $root_path.$web_path;
$prefix = "dynamic_dir_";
$today_dir = $prefix.date('Ymd');
$prev_dir = getPrevDir($path,$prefix);
if($today_dir != $prev_dir)
rename($path.$prev_dir, $path.$today_dir);
echo $web_path.$today_dir;
}
function getPrevDir($path, $prefix){
if($handle = opendir($path)){
while($node = readdir($handle)){
if(is_dir($path.$node) && preg_match("/^".$prefix.".*/", $node)){
$dir = $node;
break;
}
}closedir($handle);
}
if(empty($dir)){
echo("ERROR: Not Found Directory!!!\n");
exit;
}
return $dir;
}
Categories : PHP