使用PHP代码编辑我的centos 6网络配置文件但在网络重启后我收到错误

I am using a simple file operation on the PHP in order to edit the config file for network interface on CentOS 6.7(/etc/sysconfig/network-scripts/ifcfg-eth0 ), after change in any value and save into the config file and try to restart the network interface i get this error:

does not seem to be present, delaying initialization.
[FAILED]

my PHP code is this:

<?php

// configuration
$file = '/etc/sysconfig/network-scripts/ifcfg-eth0';

// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
file_put_contents($file, $_POST['text']);

// redirect to form again
header('Location: network.php');
exit();
}

// read the textfile
$text = file_get_contents($file);

?>
<!-- HTML form -->
<form action="" method="post">
<textarea style="width:50%; height:50%;" name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>

i need manually called the network script by command setup and do a modification in the device setting and save then i will be able to restart the network interface. appreciate if anyone help me why this issue happen while if i open the config file and edit it manually it wouldn't cause this issue.

It's most likely that the user name the server runs as (by default apache on most Red Hat-based distributions) doesn't have permission to write to/etc/sysconfig/network-scripts/ifcfg-eth0.

You should check the return values of:

file_put_contents($file, $_POST['text']);

From http://php.net/manual/en/function.file-put-contents.php

This function returns the number of bytes that were written to the file, or FALSE on failure.


I presume this script will only be run by your or trusted colleagues given that there's no validation of the user input. Indentation would also make the PHP code more readable.