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($...)
 
편집 요약 없음
19번째 줄: 19번째 줄:
     $txt = "Jane Doe\n";
     $txt = "Jane Doe\n";
     fwrite($myfile, $txt);
     fwrite($myfile, $txt);
    fclose($myfile);
?>
?>
</syntaxhighlight>
</syntaxhighlight>
28번째 줄: 27번째 줄:
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>

2023년 2월 18일 (토) 11:50 판

파일 열기

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

파일 쓰기

  • 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);
?>