PHP 파일 쓰기: 두 판 사이의 차이

IT위키
(새 문서: == 파일 열기 == * 쓰기 모드(w)로 파일을 연다. * fopen을 사용한다. <syntaxhighlight lang="php"> <?php $myFile = fopen("testfile.txt", "w") or die("Unable to open file!"); ?> </syntaxhighlight> == 파일 쓰기 == * fwrite를 이용해 파일을 쓴다. <syntaxhighlight lang="php" line="1"> <?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "John Doe\n"; fwrite($myfile, $txt); $txt = "Jane Doe\n"; fwrite($...)
 
편집 요약 없음
 
(같은 사용자의 중간 판 하나는 보이지 않습니다)
7번째 줄: 7번째 줄:
     $myFile = fopen("testfile.txt", "w") or die("Unable to open file!");
     $myFile = fopen("testfile.txt", "w") or die("Unable to open file!");
?>
?>
</syntaxhighlight>
</syntaxhighlight>'''(참고) 파일 열기 모드'''
 
* r : 읽기(read)
* w : 쓰기(write)
* a: 추가하기(append)


== 파일 쓰기 ==
== 파일 쓰기 ==
19번째 줄: 23번째 줄:
     $txt = "Jane Doe\n";
     $txt = "Jane Doe\n";
     fwrite($myfile, $txt);
     fwrite($myfile, $txt);
    fclose($myfile);
?>
?>
</syntaxhighlight>
</syntaxhighlight>
28번째 줄: 31번째 줄:
Jane Doe
Jane Doe
</pre>
</pre>
== 파일 닫기 ==
* fclose로 파일을 닫는다.
* php 파일이 끝나면 알아서 닫기지만, 다 쓰고 나면 아래와 같이 명시적으로 닫아주는 것이 좋다.
<syntaxhighlight lang="php" line="1">
<?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "John Doe\n";
    fwrite($myfile, $txt);
    $txt = "Jane Doe\n";
    fwrite($myfile, $txt);
    fclose($myfile);
?>
</syntaxhighlight>

2024년 4월 5일 (금) 18:17 기준 최신판

파일 열기[편집 | 원본 편집]

  • 쓰기 모드(w)로 파일을 연다.
  • fopen을 사용한다.
<?php
    $myFile = fopen("testfile.txt", "w") or die("Unable to open file!");
?>

(참고) 파일 열기 모드

  • r : 읽기(read)
  • w : 쓰기(write)
  • a: 추가하기(append)

파일 쓰기[편집 | 원본 편집]

  • fwrite를 이용해 파일을 쓴다.
<?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "John Doe\n";
    fwrite($myfile, $txt);
    $txt = "Jane Doe\n";
    fwrite($myfile, $txt);
?>
  • 결과는 아래와 같다. (newfile.txt)
John Doe
Jane Doe

파일 닫기[편집 | 원본 편집]

  • fclose로 파일을 닫는다.
  • php 파일이 끝나면 알아서 닫기지만, 다 쓰고 나면 아래와 같이 명시적으로 닫아주는 것이 좋다.
<?php
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "John Doe\n";
    fwrite($myfile, $txt);
    $txt = "Jane Doe\n";
    fwrite($myfile, $txt);
    fclose($myfile);
?>