2016-09-20 81 views
0

我想检查一个目录已经存在一个FTP服务器上,如果再把它存在,它仅应建立和JSON文件保存到这个目录和return true,柜面它不存在,那么它应该首先创建目录和JSON文件保存到目录,也return true否则它应该return false保存JSON到ftp目录

我目前的代码如下所示:

<?php 

    // Function to create the outfit xml file 
    function create_outfit_json(){ 
     if (!file_exists('../user/' . $this->Username)) { 
      mkdir('../user/' . $this->username, 0777, true); 

      $json['outfits'] = []; 
      $json['outfits']['0'] = [ 
       'outfit' => [ 
        'url' => 'placeholder', 
        'default' => 1, 
        'name' => 'New outfit', 
        'c' => '#bb9977', 
        'mood' => 3, 
        'species' => 'male' 
       ] 
      ]; 

      $fp = fopen('../user/' . $this->Username . '/outfits.json', 'w'); 
      fwrite($fp, json_encode($json)); 
      fclose($fp); 

      return true; 
     }else if(file_exists('../user/' . $this->Username)){ 
      $json['outfits'] = []; 
      $json['outfits']['0'] = [ 
       'outfit' => [ 
        'url' => 'placeholder', 
        'default' => 1, 
        'name' => 'New outfit', 
        'c' => '#bb9977', 
        'mood' => 3, 
        'species' => 'male' 
       ] 
      ]; 

      $fp = fopen('../user/' . $this->Username . '/outfits.json', 'w'); 
      fwrite($fp, json_encode($json)); 
      fclose($fp); 

      return true; 
     }else{ 
      return false; 
     } 
    } 

?> 

有没有办法让这个代码看起来更干净,短?

+0

尝试http://codereview.stackexchange.com/是的,那里有很多需要提高的。 – Xatenev

+0

你的if和ifelse完全一样吗? – Neat

+0

@Neat不,他有一个的mkdir如果它不上一开始就存在:P – Xatenev

回答

0
<?php 

    // Function to create the outfit xml file 
    function create_outfit_json(){ 
     if (!file_exists('../user/' . $this->Username)) { 
      mkdir('../user/' . $this->Username, 0777, true); 
     } 

      $json['outfits']['0'] = [ 
       'outfit' => [ 
        'url' => 'placeholder', 
        'default' => 1, 
        'name' => 'New outfit', 
        'c' => '#bb9977', 
        'mood' => 3, 
        'species' => 'male' 
       ] 
      ]; 

      $fp = fopen('../user/' . $this->Username . '/outfits.json', 'w'); 
      fwrite($fp, json_encode($json)); 
      fclose($fp); 
     } 
    } 

?> 

你不需要冗余代码 - 只需将mkdir包装在一个if。

+0

'$ json ['outfits'] = [];'是不必要的,而且你没有回报 – Neat

0

阅读的意见,总是写代码重用能力,

// Seperate all data and saving 
    function create_outfit_json(){ 
     $json['outfits'] = []; 
     $json['outfits']['0'] = [ 
      'outfit' => [ 
       'url' => 'placeholder', 
       'default' => 1, 
       'name' => 'New outfit', 
       'c' => '#bb9977', 
       'mood' => 3, 
       'species' => 'male' 
      ] 
     ]; 
     return $this->saveToDirectory(json_encode($json), 'outfits.json'); 
    } 

    // passing data and filename will give you flexibility 
    function saveToDirectory($data, $filename){ 
     if (!file_exists('../user/' . $this->Username)) { 
      mkdir('../user/' . $this->username, 0777, true); 
     } 
     // rather than opening connection let php do it for you 
     return file_put_contents('../user/'.$this->Username.'/'.$filename, $data); 
    }