我想檢查一個目錄已經存在一個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;
}
}
?>
有沒有辦法讓這個代碼看起來更乾淨,短?
嘗試http://codereview.stackexchange.com/是的,那裏有很多需要提高的。 – Xatenev
你的if和ifelse完全一樣嗎? – Neat
@Neat不,他有一個的mkdir如果它不上一開始就存在:P – Xatenev