Member 12990548 Ответов: 1

Загрузить изображение веб-сервиса на PHP для android и получить ответ в json ?


Я не знаю, как загрузить изображение в базу данных через PHP и получить ответ в JSON-коде.
Я использую Postman для запуска этого кода

Что я уже пробовал:

<?php 
	
	//importing dbDetails file 
	
define('HOST','localhost');
 define('USER','root');
 define('PASS','');
 define('DB','db_images');
 
	
	//this is our upload folder 
	$upload_path = 'uploads/';
	
	//Getting the server ip 
	$server_ip = gethostbyname(gethostname());
	
	//creating the upload url 
	$upload_url = 'http://'.$server_ip.'/AndroidImageUpload/'.$upload_path; 
	
	//response array 
	$response = array(); 
	
	
	if($_SERVER['REQUEST_METHOD']=='POST'){
		
		//checking the required parameters from the request 
		if(isset($_POST['name']) and isset($_FILES['image']['name'])){
			
			//connecting to the database 
			$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...');
			
			//getting name from the request 
			$name = $_POST['name'];
			$mobile = $_POST['mobile'];
			$uid = $_POST['uid'];
			$point = $_POST['point'];
			
			//getting file info from the request 
			$fileinfo = pathinfo($_FILES['image']['name']);
			
			//getting the file extension 
			$extension = $fileinfo['extension'];
			
			//file url to store in the database 
			$file_url = $upload_url . getFileName() . '.' . $extension;
			
			//file path to upload in the server 
			$file_path = $upload_path . getFileName() . '.'. $extension; 
			
			//trying to save the file in the directory 
			try{
				//saving the file 
				move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
				$sql = "INSERT INTO `db_images`.`images` (`id`, `url`, `name`,`mobile`, `uid`, `point`) VALUES (NULL, '$file_url', '$name', '$mobile', '$uid', '500');";
				
				//adding the path and name to database 
				if(mysqli_query($con,$sql)){
					
					//filling response array with values 
					$response['error'] = false; 
					$response['url'] = $file_url; 
					$response['name'] = $name;
					$response['uid'] = $uid; 
					$response['mobile'] = $mobile;
					$response['point'] = '500';
				}
			//if some error occurred 
			}catch(Exception $e){
				$response['error']=true;
				$response['message']=$e->getMessage();
			}		
			//displaying the response 
			echo json_encode($response);
			
			//closing the connection 
			mysqli_close($con);
		}else{
			$response['error']=true;
			$response['message']='Please choose a file';
		}
	}
	
	/*
		We are generating the file name 
		so this method will return a file name for the image to be upload 
	*/
	function getFileName(){
		$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...');
		$sql = "SELECT max(id) as id FROM images";
		$result = mysqli_fetch_array(mysqli_query($con,$sql));
		
		mysqli_close($con);
		if($result['id']==null)
			return 1; 
		else 
			return ++$result['id']; 
	}
	?>

1 Ответов

Рейтинг:
0

Ngo Tuong Dan

Привет парень,

URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

// theo thu tu cua lenh execute(user,pass);
String user = params[0];
String pass = params[1];

String urlParams = "username=" + URLEncoder.encode(user, "UTF-8") +
 "&password=" + URLEncoder.encode(pass, "UTF-8");

con.setRequestMethod("POST");
con.setDoOutput(true);

DataOutputStream dos = new DataOutputStream(con.getOutputStream());
// chuyern user va pass cho web server
dos.writeBytes(urlParams);
dos.flush();
dos.close();

InputStream in = con.getInputStream();

StringBuilder str = new StringBuilder();
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
 str.append(line);
 str.append("\n");
}
br.close();


PHP код здесь
<pre>if(isset($_POST['username']) && isset($_POST['password']))
{
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("android");
        $username = isset($_POST['username']) ? $_POST['username'] : "";
       //$_POST['username'];
        $password = isset($_POST['password']) ? $_POST['password'] : "";
       //$_POST['password'];
        $sqlSelect = "select username,hinh from nguoidung where username='$username'";
        $users = mysql_query($sqlSelect);
 
  if(mysql_num_rows($users) > 0)
  {
       echo json_encode(mysql_fetch_assoc($users)); 
       $_SESSION['user'] = "ok";
  }else{
        echo json_encode(array("loi"=>"user khong hop le"));
  }else
  { 
        echo json_encode(array("loi"=>"user khong hop le"));
  }
}


Можете просматривать здесь