PHP - 不安全的文件编辑,擦除所有数据

时间:2017-09-07 14:02:25

标签: javascript php jquery zend-framework ini

我有现有的ini文件,仅用于从Javascript操作进行编辑。

但PHP / Javascript会删除ini文件中的所有数据,并留下一行包含一行。 (有时它不会被删除,但当第4节或第5节编辑它时会删除整个文件并保留最后一个文件)

的config.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的:

$.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);
  }
}

1 个答案:

答案 0 :(得分:0)

延迟写入尝试修复它。

df = pd.DataFrame([[0,1,3,4,np.nan,2],[3,5,6,np.nan,3,3]])

df
   0  1  2    3    4  5
0  0  1  3  4.0  NaN  2
1  3  5  6  NaN  3.0  3

np.where(np.asanyarray(np.isnan(df)))
(array([0, 1]), array([4, 3]))