2017-06-03 55 views
0
<?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

這裏是圖片上傳代碼並獲取響應圖像url保存您的數據庫。你希望你試試這個。

<?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); 
?>