0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { //如果上传成功 if(move_uploaded_file($img_tmp,$dir_name)){ //压缩图片 //tinypng($config['tinypng'],$dir_name); $img_url = $config['domain'].$dir_name; //自定义图片路径 $img_info = getimagesize($dir_name); $img_width = $img_info['0']; //图片宽度 $img_height = $img_info['1']; //图片高度 $re_data = array("linkurl" => $img_url,width => $img_width,"height" => $img_height,"status" => 'ok'); //返回json格式 echo json_encode($re_data); recordImageInfo($img_name , $dir_name, $img_url);// 记录文件原始名称,文件路径和文件url exit; } //没有上传成功 else{ echo "上传失败!"; } } } else{ $re_data = array("linkurl" => $img_url,width => $img_width,"height" => $img_height,"status" => 'no'); //返回json格式 echo json_encode($re_data); } //判断用户是否登录,5个参数,cookie,用户名、密码、用户上传目录、管理员上传目录 function check($cookie,$user,$pass,$udir,$adir){ $loginid = $cookie; $userid = md5($user.$pass); if($loginid == $userid) { return $adir; } else { return $udir; } } // 记录图片信息并初始化数据库(如果不存在) function recordImageInfo($rawname, $path, $url) { global $config; include_once("./medoo.php"); $db = new medoo([ 'database_type' => 'sqlite', 'database_file' => $config['dbfile'] ]); $ret = $db->query("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='pictures'")->fetchAll(); if ($ret[0][0] == 0) { $db->exec("CREATE TABLE [pictures] ([id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [raw] VARCHAR, [path] VARCHAR, [url] TEXT)"); $db->exec("CREATE INDEX [url] ON [pictures] ([url])"); } $db->exec("insert into pictures (raw, path, url) values (".$db->pdo->quote($rawname).",\"$path\",\"$url\")"); } ?>