PHP fopen() 함수
❮ PHP 파일 시스템 참조예시
파일 열기, 행 읽기 - EOF에 도달할 때까지:
<?php
$file = fopen("test.txt", "r");
//Output lines until EOF is reached
while(! feof($file)) {
$line = fgets($file);
echo $line. "<br>";
}
fclose($file);
?>
정의 및 사용
fopen() 함수는 파일이나 URL을 엽니다.
참고: 텍스트 파일에 쓸 때 올바른 줄 끝 문자를 사용해야 합니다! Unix 시스템은 \n을 사용하고 Windows 시스템은 \r\n을 사용하며 Macintosh 시스템은 줄 끝 문자로 \r을 사용합니다. Windows는 파일 작업 시 \n을 \r\n으로 변환하는 변환 플래그('t')를 제공합니다. 'b'를 사용하여 바이너리 모드를 강제 실행할 수도 있습니다. 이 플래그를 사용하려면 모드 매개변수의 마지막 문자로 'b' 또는 't'를 지정하십시오.
통사론
fopen(filename, mode, include_path, context)
매개변수 값
Parameter | Description |
---|---|
filename | Required. Specifies the file or URL to open |
mode | Required. Specifies the type of access you require to the file/stream. Possible values:
|
include_path | Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well |
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream |
기술적 세부 사항
반환 값: | 성공 시 파일 포인터 리소스, FALSE 및 실패 시 오류입니다. 함수 이름 앞에 "@"를 추가하여 오류를 숨길 수 있습니다. |
---|---|
PHP 버전: | 4.3+ |
PHP 변경 로그: | PHP 7.1: "e" 옵션 추가 PHP 5.2: "c" 및 "c+" 옵션 추가 |
❮ PHP 파일 시스템 참조