PHP 파일 열기/읽기/닫기
이 장에서는 서버에서 파일을 열고, 읽고, 닫는 방법에 대해 설명합니다.
PHP 열기 파일 - fopen()
파일을 여는 더 좋은 방법은 fopen()
기능을 사용하는 것입니다. 이 기능은 기능보다 더 많은 옵션을 제공합니다 readfile()
.
수업 중에 "webdictionary.txt"라는 텍스트 파일을 사용합니다.
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
의 첫 번째 매개변수는 fopen()
열려는 파일의 이름을 포함하고 두 번째 매개변수는 파일을 열어야 하는 모드를 지정합니다. 다음 예제는 fopen() 함수가 지정된 파일을 열 수 없는 경우에도 메시지를 생성합니다.
예시
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
팁: 및 기능은 아래 fread()
에서 fclose()
설명합니다.
파일은 다음 모드 중 하나로 열릴 수 있습니다.
Modes | Description |
---|---|
r | Open a file for read only. File pointer starts at the beginning of the file |
w | Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a | Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x | Creates a new file for write only. Returns FALSE and an error if file already exists |
r+ | Open a file for read/write. File pointer starts at the beginning of the file |
w+ | Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file |
a+ | Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist |
x+ | Creates a new file for read/write. Returns FALSE and an error if file already exists |
PHP 읽기 파일 - fread()
함수 는 fread()
열린 파일에서 읽습니다.
의 첫 번째 매개변수는 fread()
읽을 파일의 이름을 포함하고 두 번째 매개변수는 읽을 최대 바이트 수를 지정합니다.
다음 PHP 코드는 "webdictionary.txt" 파일을 끝까지 읽습니다.
fread($myfile,filesize("webdictionary.txt"));
PHP 닫기 파일 - fclose()
이 fclose()
함수는 열려 있는 파일을 닫는 데 사용됩니다.
작업을 마친 후에는 모든 파일을 닫는 것이 좋은 프로그래밍 방법입니다. 리소스를 차지하는 서버에서 열린 파일이 실행되는 것을 원하지 않습니다!
fclose()
닫으려는 파일의 이름(또는 파일 이름을 보유하는 변수)이 필요합니다 .
<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>
PHP 읽기 한 줄 - fgets()
이 fgets()
함수는 파일에서 한 줄을 읽는 데 사용됩니다.
아래 예는 "webdictionary.txt" 파일의 첫 번째 줄을 출력합니다.
예시
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
참고: 함수 를 호출한 후 fgets()
파일 포인터가 다음 줄로 이동했습니다.
PHP 파일 끝 확인 - feof()
이 feof()
함수는 "파일 끝"(EOF)에 도달했는지 확인합니다.
이 feof()
함수는 길이를 알 수 없는 데이터를 반복할 때 유용합니다.
아래 예에서는 파일 끝에 도달할 때까지 "webdictionary.txt" 파일을 한 줄씩 읽습니다.
예시
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP 읽기 단일 문자 - fgetc()
이 fgetc()
함수는 파일에서 단일 문자를 읽는 데 사용됩니다.
아래 예에서는 파일 끝에 도달할 때까지 "webdictionary.txt" 파일을 문자별로 읽습니다.
예시
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
참고: 함수 호출 후 fgetc()
파일 포인터는 다음 문자로 이동합니다.
완전한 PHP 파일 시스템 참조
파일 시스템 기능에 대한 전체 참조를 보려면 전체 PHP 파일 시스템 참조 로 이동하십시오 .