1
기존의 ini
파일은 자바 스크립트 동작에서만 편집 할 수 있습니다.PHP - 안전하지 않은 파일 편집, 모든 데이터 지우기
하지만 PHP/자바 스크립트는 ini 파일의 모든 데이터를 지우고 한 섹션을 한 줄로 남겨 둡니다.
[slide1]
button0=6,298,273,425
[slide2]
button1=1005,313,1269,425
button0=6,298,273,425
[slide3]
button1=1005,313,1269,425
button0=6,298,273,425
[slide4]
button1=1005,313,1269,425
button0=6,298,273,425
[slide5]
button1=1005,313,1269,425
button0=6,298,273,425
button2=1005,313,1269,425
button3=6,298,273,425
[slide6]
[slide7]
PHP :
// EDIT only? (no erase, no delete, no new line add
function config_set($config_file, $section, $key, $value) {
$config_data = parse_ini_file($config_file, true);
$config_data[$section][$key] = $value;
$new_content = '';
foreach ($config_data as $section => $section_content) {
$section_content = array_map(function($value, $key) {
return "$key=$value";
}, array_values($section_content), array_keys($section_content));
$section_content = implode("\n", $section_content);
$new_content .= "[$section]\n$section_content\n";
}
file_put_contents($config_file, $new_content);
}
config_set('config.ini', 'slide5', "button0", "1,2,3,4,5,5,6,7,7");
echo 'updated';
jQuery를 :
config.ini 파일 (때로는 그는 삭제하지 만하면은 Section 또는 편집 5 장 그것은 마지막 하나를 전체 파일을 지우고 계속)
$.get('draw_save.php', {
test: 'none'
}, function(msg) {
console.log(msg);
});
편집 : 안전한 쓰기 시도
function safefilerewrite($fileName, $dataToSave) {
if ($fp = fopen($fileName, 'w')) {
$startTime = microtime(TRUE);
do {
$canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep
// for 0 - 100 milliseconds, to avoid collision and CPU load
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
//file was locked so now we can store information
if ($canWrite) {
fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
'$ new_content'가 비어 있다는 것을 의미하지는 않습니까? 따라서 아무것도없이 파일을 대체하십시오 ... – JustBaron
불가능합니다. 비어 있지 않기 때문에 적어도 일부는 씁니다. – YumYumYum
나는 안전한 쓰기를 시도했다. 그러나 여전히 동일합니다. 파일 데이터를 지우고 1 섹션을 유지합니다. – YumYumYum