用PHP上传Android图像Web服务并在json中获取响应? [英] Upload image web service in PHP for android and get response in json ?

查看:86
本文介绍了用PHP上传Android图像Web服务并在json中获取响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何通过PHP在数据库中上传图像并在json代码中获取响应。

我使用Postman运行该代码



我尝试过:



I don't how to upload image in database through PHP and get response in json code.
I am using Postman for run that code

What I have tried:

<?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']; 
	}
	?>

推荐答案

upload_path ='uploads /';

//获取服务器ip
upload_path = 'uploads/'; //Getting the server ip


server_ip = gethostbyname(gethostname());

//创建上传网址
server_ip = gethostbyname(gethostname()); //creating the upload url


upload_url ='http://'。
upload_url = 'http://'.


这篇关于用PHP上传Android图像Web服务并在json中获取响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆