2011-04-27 11:13
浏览 89


I need to add/remove SAMBA shares through PHP or via SSH (ssh link from PHP).

So far the only solution I've found is a class for primitively handling the file, which I haven't got very much faith in in terms of reliability. (

Could you recommend a way to do it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtsjq28482
    dtsjq28482 2011-04-27 12:15

    Can't find something useful, so I would recommend a likewise simplistic approach. Instead of finding a full-fledged INI parser, it's advisable to work in append-mostly mode. A simple regex would be sufficient to replace existing [share] sections without harming the rest of the smb.conf file.

    And you can use the testparm utility to probe for correctness before overwriting the real file.

    define("SMB_CONF", "/etc/samba/smb.conf");
    function add_share($section, $options) {
        // read old data
        #$old = parse_ini_file(SMB_CONF);
        $conf = file_get_contents(SMB_CONF);
        // merge new settings
        #if (isset($old[$section])) {
        #    $options = array_merge($old[$section], $options);
        // remove old share section, if it exists
    )+/m", "", $conf);
        // write out new ini file
        $conf .= "
        foreach ($options as $key=>$value) {
            $conf .= "$key = $value
        $tmp = tempnam("/tmp/", "SMB");
        file_put_contents($tmp, $conf);
        // copy if it is syntactically correct
        if (strstr(`testparm -s $tmp 2>&1`, "OK")) {
            rename($tmp, SMB_CONF);

    Updated Okay, parse_ini_file does not work in either case. It trips over the smb.conf style. So you can only completely replace existing entries, not update them.

    点赞 评论
  • dourukeng5302
    dourukeng5302 2011-04-27 11:40

    Zend Framework's Zend_Config setup might be useful as it's able to read/write ini files.


    点赞 评论