how to upload image on database using xampp

277 views
0
Deepak Tiwari | 14 xp

how to upload image on database using xampp

<?php<?php //importing file require_once ‘dbconnection.php’; //this is our upload folder $upload_path =’uploads/’; // getting the server ip $server_ip = gethostbyname(gethostname()); $upload_url = ‘http://’.$server_ip.’/Deepak/’.$upload_path; //response array $response = array(); if($_SERVER[’REQUEST_METHOD’]== ‘POST’) { if(isset($_POST[’name’]) and isset($_FILES[’image’][’name’])) { $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die(‘unable to connect to database’); $name = $_POST[’name’]; $fileinfo=pathinfo($_FILES[’image’][’name’]); $extension = $fileinfo[’extension’]; $file_url = $upload_url.getFileName().’.’.$extension; $upload_name=$_FILES[’image’][’name’]; $folder=”uploads/”;  //$file_url=”{$folder}{$upload_name}”; $file_path = $upload_path.getFileName(). ‘.’.$extension; try{ move_uploaded_file($_FILES[’image’][’tmp_name’], $file_path); $sql = “INSERT INTO imges(url,name) values(‘$file_url’,’$name’)”; echo $sql; if(mysqli_query($con,$sql)) { $response[’error’] = false; $response[’url’] = $file_url; $response[’name’] = $name; } }catch(Exception $e){ $response[’error’] = false; $response[’message’] = $e->getMessage(); } mysqli_close($con); }else{ $response[’error’] = true; $response[’message’] = ‘please choose a file’; } echo json_encode($response); } function getFileName() { $con= mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die(‘unable to connect’); $sql = “SELECT max(id) as id FROM  imges”; $result = mysqli_fetch_array(mysqli_query($con,$sql)); mysqli_close($con); if($result[’id’]!==null) { return 1; }else{ return ++$result[’id’]; } } ?>

0
Dholfaqar Alharazi | 40 xp

Use below code

 

in android

in php