Filesystem 헬퍼¶
디렉토리 헬퍼 파일에는 디렉토리 작업을 지원하는 기능이 있습니다.
사용 가능한 함수¶
사용 가능한 함수는 다음과 같습니다.
-
directory_map
($source_dir[, $directory_depth = 0[, $hidden = FALSE]])¶ Parameters: - $source_dir (string) – 소스 디렉토리의 경로
- $directory_depth (int) – 탐색할 디렉토리의 깊이 (0 = 완전 재귀, 1 = 현재 디렉토리 등)
- $hidden (bool) – 숨겨진 디렉토리 포함 여부
Returns: 파일의 배열
Return type: array
Examples:
$map = directory_map('./mydirectory/');
Note
경로는 index.php 파일을 기준으로 합니다.
디렉토리에 포함된 하위 폴더도 매핑됩니다. 재귀 깊이를 제어하려면 두 번째 매개 변수 (정수)를 사용하면됩니다. 깊이 1은 최상위 디렉토리만 매핑합니다.
$map = directory_map('./mydirectory/', 1);
기본적으로 숨겨진 파일은 반환된 배열에 포함되지 않습니다. 이 동작을 재정의하기 위해 세 번째 매개 변수를 true(부울)로 설정할 수 있습니다.
$map = directory_map('./mydirectory/', FALSE, TRUE);
각 폴더 이름은 배열 색인이며 포함된 파일은 숫자로 색인됩니다. 다음은 일반적인 배열의 예입니다.
Array ( [libraries] => Array ( [0] => benchmark.html [1] => config.html ["database/"] => Array ( [0] => query_builder.html [1] => binds.html [2] => configuration.html [3] => connecting.html [4] => examples.html [5] => fields.html [6] => index.html [7] => queries.html ) [2] => email.html [3] => file_uploading.html [4] => image_lib.html [5] => input.html [6] => language.html [7] => loader.html [8] => pagination.html [9] => uri.html ) )
결과가 없으면 빈 배열을 반환합니다.
-
write_file
($path, $data[, $mode = 'wb'])¶ Parameters: - $path (string) – 파일 경로
- $data (string) – 파일에 쓸 데이터
- $mode (string) –
fopen()
모드
Returns: 쓰기에 성공하면 TRUE, 오류가 발생하면 FALSE
Return type: bool
경로에 지정된 파일에 데이터를 씁니다. 파일이 존재하지 않으면 함수가 파일을 작성합니다.
Example:
$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
이 함수가 파일에 데이터를 쓰려면 쓰기 가능하도록 권한을 설정해야합니다. 파일이 존재하지 않으면 파일을 포함하는 디렉토리는 쓰기 가능해야합니다.
Note
경로는 컨트롤러나 뷰 파일이 아닌 사이트의 index.php 파일을 기준으로합니다. CodeIgniter는 프론트(front) 컨트롤러를 사용하므로 경로는 항상 사이트 index를 기준으로 합니다.
Note
이 기능은 파일에 쓰는 동안 파일에 대한 잠금(exclusive lock)을 획득합니다.
-
delete_files
($path[, $delDir = FALSE[, $htdocs = FALSE[, $hidden = FALSE]]]])¶ Parameters: - $path (string) – 디렉토리 경로
- $delDir (bool) – 디렉토리 삭제 여부
- $htdocs (bool) – .htaccess 및 색인 페이지 파일 삭제를 건너 뛸지 여부
- $hidden (bool) – 숨김 파일 삭제 여부 (마침표(.)로 시작하는 파일)
Returns: 성공시 TRUE, 오류 발생시 FALSE
Return type: bool
제공된 경로에 포함된 모든 파일을 삭제합니다.
Example:
delete_files('./path/to/directory/');
두 번째 매개 변수가 TRUE로 설정되면 제공된 루트 경로에 포함된 모든 디렉토리도 삭제됩니다.
Example:
delete_files('./path/to/directory/', TRUE);
Note
파일을 삭제하려면 시스템에서 파일을 쓸 수 있거나 소유해야합니다.
-
get_filenames
($source_dir[, $include_path = FALSE])¶ Parameters: - $source_dir (string) – 디렉토리 경로
- $include_path (bool|null) – 파일 이름의 일부로 경로를 포함할지 여부; 경로가 없을때
false
, $source_dir인 경우null
, 전체 경로일때true
- $hidden (bool) – 숨겨진 파일 포함 여부 (마침표(.)로 시작하는 파일)
Returns: 파일 이름의 배열
Return type: array
서버 경로를 입력으로 사용하고 여기에 포함된 모든 파일의 이름이 포함된 배열을 반환합니다. 상대 경로의 경우 두 번째 매개 변수를 ‘relative’로 설정하거나, 전체 파일 경로를 비어 있지 않은 다른 값으로 설정하여 파일 이름에 선택적으로 파일 경로를 추가할 수 있습니다.
Example:
$controllers = get_filenames(APPPATH.'controllers/');
-
get_dir_file_info
($source_dir, $top_level_only)¶ Parameters: - $source_dir (string) – 디렉토리 경로
- $top_level_only (bool) – 지정된 디렉토리의 하위 디렉토리 제외 여부
Returns: 제공된 디렉토리의 내용에 대한 정보를 포함하는 배열
Return type: array
지정된 디렉토리를 읽고 파일 이름, 파일 크기, 날짜 및 권한을 포함하는 배열을 만듭니다. 지정된 경로에 포함된 하위 폴더는 두 번째 매개 변수를 FALSE로 전달한 경우에만 읽힙니다. 이는 많은 주의를 기울여 하는 작업이 될 수 있기 때문입니다.
Example:
$models_info = get_dir_file_info(APPPATH.'models/');
-
get_file_info
($file[, $returned_values = ['name', 'server_path', 'size', 'date']])¶ Parameters: - $file (string) – 파일 경로
- $returned_values (array|string) – 배열 또는 쉼표로 구분된 문자열로 전달하기 위해 반환할 정보 유형
Returns: 지정된 파일에 대한 정보가 포함된 배열, 실패시 FALSE
Return type: array
파일 및 경로가 제공되면 파일의 name, path, size, date modified 정보 속성을 (선택적으로) 반환합니다. 두 번째 매개 변수를 사용하면 반환할 정보를 명시적으로 선언할 수 있습니다.
유효한
$returned_values
옵션: name, size, date, readable, writeable, executable, fileperms.
-
symbolic_permissions
($perms)¶ Parameters: - $perms (int) – 권한(Permission)
Returns: 심볼릭(Symbolic) 권한 문자열
Return type: string
숫자 사용 권한(예:
fileperms()
에 의해 반환된)을 사용하여 파일 사용 권한의 표준 기호를 반환합니다.echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r--
-
octal_permissions
($perms)¶ Parameters: - $perms (int) – 권한
Returns: 8진수 권한 문자열
Return type: string
숫자 사용 권한(예:
fileperms()
에 의해 반환된)을 사용하여 파일 권한의 8진수 표기법를 반환합니다.echo octal_permissions(fileperms('./index.php')); // 644
-
set_realpath
($path[, $check_existence = FALSE])¶ Parameters: - $path (string) – 경로
- $check_existence (bool) – 경로가 실제로 존재하는지 확인
Returns: 절대 경로
Return type: string
이 기능은 심볼릭 링크나 상대 디렉터리 구조가 없는 서버 경로를 반환합니다. 경로를 확인할 수 없는 경우 선택적 두 번째 인수로 인해 오류가 트리거됩니다.
Examples:
$file = '/etc/php5/apache2/php.ini'; echo set_realpath($file); // Prints '/etc/php5/apache2/php.ini' $non_existent_file = '/path/to/non-exist-file.txt'; echo set_realpath($non_existent_file, TRUE); // Shows an error, as the path cannot be resolved echo set_realpath($non_existent_file, FALSE); // Prints '/path/to/non-exist-file.txt' $directory = '/etc/php5'; echo set_realpath($directory); // Prints '/etc/php5/' $non_existent_directory = '/path/to/nowhere'; echo set_realpath($non_existent_directory, TRUE); // Shows an error, as the path cannot be resolved echo set_realpath($non_existent_directory, FALSE); // Prints '/path/to/nowhere'