Home > Software engineering >  How to upload image from local device to server using API in php
How to upload image from local device to server using API in php

Time:10-09

I want to make an API that takes a photo from a user app and upload it to my server through API

CodePudding user response:

I think you should try this one code I had used eralier in my projects

<?php

if (isset($_POST['property_id']))
{ 
    $property_id = $_POST['property_id'];

    $filename = $_FILES['image']['name'];
    $filedata = $_FILES['image']['tmp_name'];
    $filesize = $_FILES['image']['size'];

    if ($filedata != '')
    {
        $target_dir = "images/".$property_id."/";
        $target_file = $target_dir . basename($filename);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

        $check = getimagesize($filedata);

        if($check !== false) {
            // echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {

            $msg = "File is not an image.";
            $uploadOk = 0;

            $double = array(
                'msg' => $msg,
                'statusCode' => 201,
            );
            
            echo json_encode($double);
        }

        // Check if file already exists
        if (file_exists($target_file)) {

            $msg = "Sorry, file already exists.";
            $uploadOk = 0;

            $double = array(
                'msg' => $msg,
                'statusCode' => 201, 
            );
            
            echo json_encode($double);
        }

        // Check file size
        if ($filesize > 500000) {

            $msg = "Sorry, your file is too large.";
            $uploadOk = 0;

            $double = array(
                'msg' => $msg,
                'statusCode' => 201, 
            );
            
            echo json_encode($double);
        }

        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {

            $msg = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;

            $double = array(
                'msg' => $msg,
                'statusCode' => 201, 
            );
            
            echo json_encode($double);

        }

        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {


        } else {
            if (move_uploaded_file($filedata, $target_file)) {

                include('connection.php');

                $insert_sql = "INSERT INTO `tbl_images`( `property_id`, `img_source`) VALUES ('$property_id','$filename')";

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

                    $alert = "Property Image Added Successfully";

                    $double = array(
                        'msg' => $alert,
                        'statusCode' => 200,
                        // 'data' => array(
                        //  array(
                        //      'property_id' => $property_id
                        //  )
                        // ) 
                    );
                    
                    echo json_encode($double);

                }
                else {
                    $alert = mysqli_error($conn);

                    $double = array(
                                    'msg' => $alert,
                                    'statusCode' => 201
                                    );

                    echo json_encode($double);
                }

                include ('disconnect.php');

             //    $msg = "The file ". htmlspecialchars( basename( $filename)). " has been uploaded.";

                // $double = array(
                //  'msg' => $msg,
                //  'statusCode' => 200, 
                // );
                
                // echo json_encode($double);
          } else {

                $msg = "Sorry, there was an error uploading your file.";

                $double = array(
                    'msg' => $msg,
                    'statusCode' => 201, 
                );
                
                echo json_encode($double);
          }
        }
    }
    else
    {
        $msg = "Please select the file";

        $double = array(
            'msg' => $msg,
            'statusCode' => 201, 
        );
        
        echo json_encode($double);
    }
}

?>

Thank me later

  • Related