레이블이 NAS인 게시물을 표시합니다. 모든 게시물 표시
레이블이 NAS인 게시물을 표시합니다. 모든 게시물 표시

2014년 5월 3일 토요일

PHP 로 파일 검색 기능 구현

가끔 서버내의 파일 목록을 조회 해 올 필요가 있다.
내 경우는 운영중의 나스 내에 파일이 정확히 업로드 되어 있는지 확인 하는 경우 사용한다.

물론 나스가 기본적으로 제공하는 파일 탐색기를 사용해도 되겠으나, 무겁기도 하거니와 파일 검색 기능은 제대로 동작하지 않는 경우가 많다.

일반적으로 판매중인 나스들이 PHP 를 지원하므로 PHP 로 간단히 특정 파일을 검색 하여 파일 목록을 조회하는 기능을 만들면 편리하다.

참고한 원본 :
http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=qna_function&wr_id=186648


<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,target-densitydpi=medium-dpi"/>

<?
 $s01 = $_REQUEST['s01'];//검색파일이름 지정
 $s02 = $_REQUEST['s02'];//검색파일이름 지정
?>

<form  action="_FileList.php" method="post">
<table>
<tr >
  <td>
   <input  type="text" name="s01" size="18" value="<?=$s01?>" />
   <input  type="text" name="s02" size="18" value="<?=$s02?>" />
  </td>
</tr>
<tr>
  <td>
   <input type="submit" value="검색" />
  </td>
</tr>
</table>

<?
$path = "./"; //검색할 폴더 지정


function get_files($path, $s01="") 
{
 $invalid_char = array("\\", "*", "+", "/", "\"", "?", "\|");

 global $s01, $s02;
 $s01 = $_REQUEST['s01'];//검색파일이름 지정
 $s02 = $_REQUEST['s02'];//검색파일이름 지정

 if (strlen($s01) <= 2) {
  echo '검색 문자열이 너무 짧습니다. (01)<br>';
  exit; 
 }

 if (strlen($s01) >= 30) {
  echo '검색 문자열이 너무 깁니다. (01)<br>';
  exit; 
 }

 if (strlen($s02) > 0) {
  if (strlen($s02) < 2) {
   echo '검색 문자열이 너무 짧습니다. (02)<br>';
   exit; 
  }

  if (strlen($s02) >= 30) {
   echo '검색 문자열이 너무 깁니다. (02)<br>';
   exit; 
  }
 }


    static $list = array(); 
 // invalid_char에 해당 하는 검색어는 잘못된 파일 형식이므로 무시함.
 $s01 = str_replace($invalid_char, "", $s01);
 $s02 = str_replace($invalid_char, "", $s02);

 // 패턴 검사를 위해 test(0)[1].gif 등의 형식을 \( \[의 형식으로 변환함. 
 $escape = array("("=>"\\(", 
      ")"=>"\\)", 
      "["=>"\\[", 
      "]"=>"\\]" 
    );

    $dh = opendir($path); 
    while ( ($read=readdir($dh))!==false ) 
    { 
  if($read[0] == '.' || $read[0] == '..' ) continue; 
  
  if ( is_dir($path.$read.'/') ) get_files($path.$read.'/'); 
  else {

   // 대소문자 구분없이 파일명에 검색어가 있는지 비교함
   if(eregi(str_replace(array_keys($escape), array_values($escape), $s01), $read))
   {
     if (strlen($s02) > 0)
     {
      if(eregi(str_replace(array_keys($escape), array_values($escape), $s02), $read))
      {
       $ext = strtolower(substr($read, (strrpos($read, '.') + 1 )));
       $read = iconv('euckr', 'utf-8', $read);
       $list[] = $read . " : " . filesize($path.$read) ;
      }
     }
     else
     {
      $ext = strtolower(substr($read, (strrpos($read, '.') + 1 )));
      $read = iconv('euckr', 'utf-8', $read);
      $list[] = $read . " : " . filesize($path.$read) ;
     }
   }
  }
    } 
    closedir($dh); 
    return $list; 
} 

$list = get_files($path);
sort($list); 
?>

<?=$s01?>, <?=$s02?>로 검색하여 총 <?=count($list)?>개를 찾았습니다.<br>

<?
 echo "<table border='1'>";
 foreach ($list as $value)
 {
  $temp = explode(':',$value); // |로 나누기 

  echo "<tr><td width='300'>$temp[0]</td><td width='100' align='right'>$temp[1]</td></tr>";
 }
 echo "</table>";
?>



2014년 4월 11일 금요일

ReadyNAS에 Apache + PHP 를 설치 하는 법.


ReadyNas 는 웹서버 기능을 제공하지만, 공식 사이트에서 지원하는 아파치 웹서버 설치는 유료다.


그러나, ReadyNas 가 리눅스인데다 ssh 접속이 가능함으로 인해, 직접 수동으로 설치가 가능하다.

1. ssh로 root 접속

2. 패키지를 업데이트 후 php5 모듈을 설치.

# apt-get update
# apt-get install libapache2-mod-php5

3. httpd.conf 편집.

# vi /etc/frontview/apache/httpd.conf
DirectoryIndex redirect.html index.html index.htm

위라인을 찾아 아래와 같이 수정한다.

DirectoryIndex redirect.html index.html index.htm index.php

그리고 LoadModule 을 검색해서 맨 아래에 PHP Module 추가.

LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

AddType application/x-tar .tgz

아래에 AddType을 추가입력한다.

AddType application/x-httpd-php .php .htm .html .inc .ph .php4 .php5 .PHP .php3

4. Shares.conf 파일 수정

Alias "/Webroot" "/c/Webroot"

  Options Indexes
  Order allow,deny
  Allow from all


RewriteEngine on
RewriteRule ^/$ /Webroot [R,L]

5. apache damon을 kill시킨후 다시 재가동

# killall apache-ssl
# apache-ssl -f /etc/frontview/apache/httpd.conf

6. /c/Webroot/ 에 테스트용 index.php 파일 생성

#vi index.php
<?
   phpinfo();
?>

7. 웹브라우져에서 http://나스IP/Webroot 를 입력해 작동 확인

8. 끝