파일(File Helper)

파일 헬퍼는 파일을 조작하는데 도움이 되는 함수들을 제공합니다.

헬퍼 로딩 Loading this Helper

헬퍼는 아래와 같이 로드합니다:

$this->load->helper('file');

사용 가능한 함수들 Available Functions

아래의 함수들이 사용 가능합니다:

read_file($file)
인수:
  • $file (string) – File path
반환값:

File contents or FALSE on failure

반환형:

문자열

지정된 파일의 데이터를 리턴합니다.

예제:

$string = read_file('./path/to/file.php');

경로는 상대경로나 절대경로 모두 됩니다. 실패시 FALSE를 리턴합니다.

Note

경로는 메인 index.php 파일로부터의 상대경로이며, 컨트롤러나 뷰 파일로부터의 상대경로는 아닙니다. CodeIgniter 는 프론트 컨트롤러를 사용하므로, 경로는 항상 메인 사이트 인덱스로부터의 상대경로입니다.

Note

이 함수는 DEPRECATED 되었습니다. 대신 PHP 내장 함수인 file_get_contents() 를 사용하여 주세요.

Important

서버가 open_basedir 를 제한하고 있다면 이 함수는 작동하지 않을것입니다.

write_file($path, $data[, $mode = 'wb'])
인수:
  • $path (string) – File path
  • $data (string) – Data to write to file
  • $mode (string) – fopen() mode
반환값:

TRUE if the write was successful, FALSE in case of an error

반환형:

bool

파일을 지정된 경로에 씁니다. 파일이 존재하지 않을 경우 파일을 생성합니다.

예제:

$data = 'Some file data';
if ( ! write_file('./path/to/file.php', $data))
{
        echo 'Unable to write the file';
}
else
{
        echo 'File written!';
}

세 번째 파라미터로 쓰기모드를 지정할 수 있습니다:

write_file('./path/to/file.php', $data, 'r+');

기본값은 ‘wb’입니다. PHP 사용자가이드 를 보시면 모드에 대해 보다 많은 정보를 얻으실 수 있습니다.

Note

경로는 메인 index.php 파일로부터의 상대경로이며, 컨트롤러나 뷰 파일로부터의 상대경로는 아닙니다. CodeIgniter 는 프론트 컨트롤러를 사용하므로, 경로는 항상 메인 사이트 인덱스로부터의 상대경로입니다.

Note

이 함수는 파일을 쓰는 동안 배타적으로 잠금기능을 실행합니다.

delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]])
인수:
  • $path (string) – Directory path
  • $del_dir (bool) – Whether to also delete directories
  • $htdocs (bool) – Whether to skip deleting .htaccess and index page files
반환값:

TRUE on success, FALSE in case of an error

반환형:

bool

지정한 경로내의 모든 파일을 지웁니다.

예제:

delete_files('./path/to/directory/');

두 번째 파라미터가 true로 설정되어 있으면, 포함하고 있는 모든 디렉토리까지 삭제합니다.

예제:

delete_files('./path/to/directory/', TRUE);

Note

파일들이 쓰기 가능한 상태거나, 그 소유자가 시스템(system)이라야 삭제가 가능합니다.

get_filenames($source_dir[, $include_path = FALSE])
인수:
  • $source_dir (string) – Directory path
  • $include_path (bool) – Whether to include the path as part of the filenames
반환값:

An array of file names

반환형:

배열

경로를 입력받아 그 안에 있는 파일 목록을 배열로 리턴합니다. 두 번째 파라미터를 TRUE로 설정하면 파일 경로도 같이 붙습니다.

예제:

$controllers = get_filenames(APPPATH.'controllers/');
get_dir_file_info($source_dir, $top_level_only)
인수:
  • $source_dir (string) – Directory path
  • $top_level_only (bool) – Whether to look only at the specified directory (excluding sub-directories)
반환값:

An array containing info on the supplied directory’s contents

반환형:

배열

지정된 디렉토리를 읽은 후 파일명, 크기, 날짜, 권한 등을 담은 배열을 리턴합니다. 두 번째 파라미터를 통해서 하위 디렉토리들도 같이 읽을지 여부를 결정합니다. 두 번째 파라미터인 $top_level_only 를 FALSE로 설정한 경우에만 하위 디렉토리들을 읽습니다.

예제:

$models_info = get_dir_file_info(APPPATH.'models/');
get_file_info($file[, $returned_values = array('name', 'server_path', 'size', 'date')])
인수:
  • $file (string) – File path
  • $returned_values (배열) – What type of info to return
반환값:

An array containing info on the specified file or FALSE on failure

반환형:

배열

파일경로가 주어지면, 파일명,경로,크기,수정된 날짜를 리턴합니다. 두 번째 파라미터로 리턴받기를 원하는 정보가 무엇인지 설정할 수 있습니다;

유효한 $returned_values 옵션은 name, size, date, readable, writeable, executable 그리고 fileperms 입니다.

get_mime_by_extension($filename)
인수:
  • $filename (string) – File name
반환값:

MIME type string or FALSE on failure

반환형:

문자열

파일 확장자를 config/mimes.php 에 기록된 마임타입(mime type )으로 변환합니다. 타입을 결정할 수 없을 경우 마임설정 파일을 열거나 FALSE를 반환합니다.

$file = 'somefile.png';
echo $file.' is has a mime type of '.get_mime_by_extension($file);

Note

이 방법은 정확하게 마임 타입을 식별하는 방법은 아닙니다. 단지 편의를 위한 것입니다. 보안이나 정확성이 필요한 상황에서는 사용하지 않는 것이 좋습니다.

symbolic_permissions($perms)
인수:
  • $perms (int) – Permissions
반환값:

Symbolic permissions string

반환형:

문자열

숫자로 권한을 입력 받아서 표준 권한 표기법에 맞는 권한을 리턴합니다. fileperms() 함수가 리턴하는 숫자를 사용하시면 됩니다.

echo symbolic_permissions(fileperms('./index.php'));  // -rw-r--r--
octal_permissions($perms)
인수:
  • $perms (int) – Permissions
반환값:

Octal permissions string

반환형:

문자열

숫자로 권한을 입력받아 3글자로 된 8진수 권한 표기법을 리턴합니다.fileperms() 함수가 리턴하는 숫자를 사용하시면 됩니다.

echo octal_permissions(fileperms('./index.php')); // 644