Neo Neo - 4 months ago 10
Android Question

how to Save image in sever folder

This is my php code for image upload.with some text.This script is used in an android app code.
Image id ,Image url and text save in database correctly but image not saved in server folder. Why is happen?

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

$image = $_FILES['image'];
$fullname = $_POST['fullname'];

require_once('dbConnect.php');

$sql ="SELECT id FROM uploadfinding ORDER BY id ASC";

$res = mysqli_query($conn,$sql);

$id = 0;

while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}

$path = "uploads/$id.png";

$actualpath = "My_url/$path";
// query for db
$sql = "INSERT INTO uploadfinding (image,fullname) VALUES ('$actualpath','$fullname')";

if(mysqli_query($conn,$sql)){

file_put_contents($actualpath,base64_decode($image));
echo "Successfully Uploaded";
}

mysqli_close($conn);
}else{
echo "Error";
}

?>

Neo Neo
Answer

This one is the working code for this question

<?php

     header("content-type:application/json");
    require_once('dbConnect.php');
    if($_SERVER['REQUEST_METHOD']=='POST'){

     if(isset($_FILES['image'])){

      $errors= array();

      $fullname = $_POST['fullname'];
      $location = $_POST['location'];

      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $date=date('Y-m-dH:i:s');
     $path="folder_name/".$date.$file_name;
     $actualpath = "http:myUrl/uploadfinding/$path";
      if(empty($errors)==true){
           move_uploaded_file($file_tmp,$path);
          $sql = "INSERT INTO uploadfinding (image,fullname,location,) VALUES ('$actualpath','$fullname','$location',)";
          if(mysqli_query($conn,$sql)){

             echo "message";
          }
      }else{
         print_r($errors);
      }


   }
    }
    ?>
Comments