使用android studio在数据库中上传图片

时间:2017-06-03 17:03:25

标签: php android mysql

<?php ## Heading ## 
  require_once 'include/db_connection.php';
  global $connection; $upload_path = 'uploads/';

  //this is our upload folder
  $server_ip = gethostbyname(gethostname());

  //Getting the server ip
  $upload_url = 'http://'.$server_ip.'/android_upload/'.$upload_path;

  //upload url
  //response array
  $response = array();
  if($_SERVER['REQUEST_METHOD']=='POST'){
    //checking the required parameters from the request
    if(isset($_POST['caption'])) {
      $caption = $_POST['caption'];
      $fileinfo = pathinfo($_FILES['image']['name']);
      //getting file info from the request
      $extension = $fileinfo['extension'];
      //getting the file extension
      $file_url = $upload_url . getFileName() . '.' . $extension;
      //file url to store in the database
      $file_path = $upload_path . getFileName() . '.'. $extension;
      //file path to upload in the server
      $img_name = getFileName() . '.'. $extension;

      //file name to store in the database
      try{
          move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
          //saving the file to the uploads folder;
          //adding the path and name to database
          $sql = "INSERT INTO photos(photo_name, photo_url, caption) ";
          $sql .= "VALUES ('{$img_name}', '{$file_url}', '{$caption}');";

          if(mysqli_query($connection,$sql)){
            //filling response array with values
            $response['error'] = false;
            $response['photo_name'] = $img_name;
            $response['photo_url'] = $file_url;
            $response['caption'] = $caption;
          }
          //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($connection);
  }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 uploaded */
  function getFileName(){
    global $connection;
    $sql = "SELECT max(id) as id FROM photos";
    $result = mysqli_fetch_array(mysqli_query($connection, $sql));
    if($result['id']== null) return 1;
    else return ++$result['id'];
    mysqli_close($connection);
  }
?>

1 个答案:

答案 0 :(得分:0)

这是图片上传代码并获取响应图片网址保存您的数据库。你希望你试试这个。

<?php
include('confi.php');
/**********MYSQL Settings****************/
function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
if($_SERVER['REQUEST_METHOD'] == "POST"){
if (isset($_FILES["file"]["name"])) {
    $file_name=$_FILES["file"]["name"];
    $temp_name=$_FILES["file"]["tmp_name"];
    $imgtype=$_FILES["file"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("m-Y")."-".rand().$_FILES['file']['name'];
    $target_path ='images/'.$imagename;
    if(move_uploaded_file($temp_name, $target_path)) {
    $result = $url.'api/'.$target_path;
    $json = array( "status" =>1,"file-url" => $result, "files" => $_FILES);
}else{
    if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
           $json = array( "status" =>0, "error" => $_FILES['uploaded']);
        }
        else{
        $json = array( "status" =>0,"files" => $_FILES);
        }
}
}
else{
   $json = array("status" =>0, "error" => "Problem occurred");
} 
}else{
        $json = array("status" => 0, "message" => "Request method is wrong!");
     }

    header('Content-type:application/json');
    echo json_encode($json);
?>