为了快速满足安卓客户端或跨域文件上传接口,现在用PHP写个可以文件上传类。可通过安卓客户端或者其他域名的网站上传窗口,可以把文件上传指定的服务器中,下面是详细代码。
//主方法获取上传文件 public function getImgByAndroid(){ $date_url=date("Ymd");//防止文件过多,加个时间文件夹便于区分 $target_path = "./public/uploads/client/user_thumb/".$date_url.'/'; $this->createPath($target_path);//因为php 程序执行的所有者是www 而目录的所有者是root,两个所有者不同,为了防止创建日期文件夹失败,加个创建方法 // die(var_dump($_FILES)); // $target_path = $target_path.$_FILES['file']['name']; if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path.$_FILES['file']['name'])) {//$_FILES['file']['tmp_name']临时文件夹 echo "The file ". $_FILES['file']['name']. " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!" . $_FILES['file']['error']; } } //根据路径创建文件夹 public function createPath($path){ $pathArr = explode("/", $path); $pathed=""; foreach ($pathArr as $key=>$row) { $pathed=$pathed.$row."/"; if ($key==0) { continue; } if (is_dir($pathed)) { continue; } else{ mkdir($pathed); } } }