Created
August 7, 2013 01:53
-
-
Save HituziANDO/6170544 to your computer and use it in GitHub Desktop.
ファイル入出力スニペットです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function read_file ($filename) | |
{ | |
$str = null; | |
$fp = fopen($filename, 'r'); | |
if ($fp) { | |
if (flock($fp, LOCK_EX)) { | |
$str = ''; | |
while ($buff = fgets($fp)) { | |
$str .= $buff; | |
} | |
flock($fp, LOCK_UN); | |
} else { | |
// ファイルロック失敗 | |
} | |
fclose($fp); | |
} | |
return $str; | |
} | |
function write_file ($filename, $str) | |
{ | |
$ret; | |
$fp = fopen($filename, 'w'); | |
if ($fp) { | |
if (flock($fp, LOCK_EX)) { | |
rewind($fp); // フィルポインタを先頭に | |
if (fputs($fp, $str) === FALSE) { | |
// 書込み失敗 | |
$ret = false; | |
} else { | |
// 書込み成功 | |
$ret = true; | |
} | |
flock($fp, LOCK_UN); | |
} else { | |
// ファイルロック失敗 | |
$ret = false; | |
} | |
} | |
fclose($fp); | |
return $ret; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment