1
私は既存のini
ファイルをJavascriptアクションから編集するためにのみ持っています。PHP - 安全でないファイルの編集、すべてのデータの消去
しかし、PHP/Javascriptでは、iniファイル内のすべてのデータが消去され、1つのセクションが1行になります。
[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(時にはそのは消去ませんが、ときsection4またはその編集5章では、最後の1をファイル全体を消去しておきます)
$.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