ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [PHP] 25. 파일 열기/읽기/닫기(File Open/Read/Close) - PHP 강좌, PHP5
    Web/PHP 2015. 6. 13. 13:54

    PHP 5 File Open/Read/Close


    1. PHP Open File - fopen()
     파일을 여는 좋은 메소드는 fopen() 함수입니다. 이 함수는 readfile() 함수보다 많은 옵션을 줍니다.

     이전 장에서 작성한 webdictionary.txt 파일을 그대로 씁니다.

     fopen()함수의 첫 파라미터는 열려고 하는 파일의 이름을 나타내고, 둘째 파라미터는 열릴 파일에 대한 모드를 명시합니다.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!DOCTYPE html>
    <html>
    <body>
     
    <?php
    $myfile = fopen("webdictionary.txt""r") or die("Unable to open file!");
    echo fread($myfile,filesize("webdictionary.txt"));
    fclose($myfile);
    ?>
     
    </body>
    </html>
    cs




     




     파일은 아래의 모드중 하나로 열 수 있습니다:

     

    ModesDescription
    rOpen a file for read only. File pointer starts at the beginning of the file
    wOpen 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
    aOpen 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
    xCreates 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

     [ 출처: W3Schools ]





    2. PHP Read File - fread()
     fread() 함수는 열린 파일로 부터 읽습니다.

     첫 파라미터는 읽을 파일에 대한 이름을 나타내고 둘째 파라미터는 읽을 최대 바이트 수를 명시합니다.

     아래 PHP 코드는 webdictionary.txt 파일의 끝까지 읽는 코드입니다.


    1
    fread($myfile,filesize("webdictionary.txt"));
    cs

     




    3. PHP Close File - fclose()
     fclose() 함수는 열린 파일을 닫는데 사용합니다.

     * 파일 작업이 끝난 뒤 모든 파일을 닫는 습관은 좋은 프로그래밍 습관입니다. 

     fclose()는 닫기를 원하는 파일의 이름을 필수로 받습니다.


     

    1
    2
    3
    4
    5
    <?php
    $myfile = fopen("webdictionary.txt""r");
    // some code to be executed....
    fclose($myfile);
    ?>
    cs







    4. PHP Read Single Line - fgets()
     fget() 함수는 파일로 부터 한 라인만을 읽어들이는데 사용합니다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!DOCTYPE html>
    <html>
    <body>
     
    <?php
    $myfile = fopen("webdictionary.txt""r") or die("Unable to open file!");
    echo fgets($myfile);
    fclose($myfile);
    ?>
     
    </body>
    </html>
    cs

     













     노트: fgets() 함수가 호출되고 난 뒤, 파일 포인터는 다음 라인으로 이동해 있습니다.


    5. PHP Check End-Of-File - feof()
     feof() 함수는 파일이 끝에 도달했는지 체크하는 함수입니다.
     feof() 함수는 데이터의 길이를 모를 때 for 반복문에서 유용합니다.

     아래 예제는 텍스트 파일을 라인단위로 읽어서 파일의 끝까지 도달할때까지 출력하는 예제입니다:



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!DOCTYPE html>
    <html>
    <body>
     
    <?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);
    ?>
     
    </body>
    </html>
    cs

     



     









     
    6. PHP Read Single Character - fgetc()
     fgetc() 함수는 파일로부터 한 문자씩 읽는데 사용됩니다.

     아래 예제는 텍스트 파일을 문자 단위로 읽어서 끝에 도달 할 때까지 출력하는 예제입니다:

    1
    2
    3
    4
    5
    6
    7
    8
    <?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);
    ?>
    cs
     
     
     



     


    * 이 강좌는 'w3schools'를 참조하여 작성하였습니다.

    댓글

Designed by Tistory.