我想检查一个目录已经存在一个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