2016-09-20 121 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); 
    }