How to Create PHP rest api for uploading files from any device?

Viewed 698

I am creating an API to register a user. I'm successfully able to register but the image is not uploading. I make some searches on google and got some info about file_put_contet() but don't know the right way to implement it

 <?php

header('Content-Type: application/json');
header('Acess-Control-Allow-Origin: *');
header('Acess-Control-Allow-Methods: POST');
header('Acess-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization');

$data=json_decode(file_get_contents("php://input"),true);

    $p_name=$data['p_name'];
    $gaurdian=$data['gaurdian'];
    $document=$data['document'];
    $card_no=$data['card_no'];
    $state=$data['state'];
    $district=$data['district'];
    $village=$data['village'];
    $pin=$data['pin'];
    $phone=$data['phone'];
    $email=$data['email'];
    $gender=$data['gender'];
    $t_name=$data['team_name'];

    $passport_photo = $_FILES['photo1']['name'];
    $temp_name1=$_FILES['photo1']['tmp_name'];
    $size1=$_FILES['photo1']['size'];
    $type1=$_FILES['photo1']['type'];

    $file_ext=strtolower(end(explode('.',$passport_photo)));
    $ext=array("jpeg","jpg","png");

    if(in_array($file_ext,$ext) === false){
        echo'<script>alert("This file type is not allowed, please choose a jpg or png")</script>';
    }

    if($size1 > 50000){
        echo'<script>alert("File size must be 150kb")</script>';
        die();
    }

    $path = "images/passport_photo/".$passport_photo;


    $card_photo=$_FILES['photo2']['name'];
    $temp_name2=$_FILES['photo2']['tmp_name'];
    $size2=$_FILES['photo2']['size'];
    $type2=$_FILES['photo2']['type'];

    $file_ext1=strtolower(end(explode('.',$card_photo)));
    $ext1=array("jpeg","jpg","png","pdf");

    if(in_array($file_ext1,$ext1) === false){
        echo'<script>alert("This file type is not allowed, please choose a jpg, png or pdf file.")</script>';
        die();
    }

    if($size2 > 150000){
        echo'<script>alert("File size must be 150kb")</script>';
        die();
    }

    $path1 = "images/document_photo/".$card_photo;

        if(move_uploaded_file($temp_name1,$path) && move_uploaded_file($temp_name2,$path1)){

            echo $query="INSERT INTO `player_reg` (`age_category`, `player_name`, `gaurdian_name`, `doc_type`, `card_no`, `state`, `district`, `town`,`pincode`, `image`, `doc_img`, `phone`, `email`, `gender`, `sport`, `team_name`,`date`) VALUES ('{$_SESSION["age_cat"]}','{$p_name}','{$gaurdian}','{$document}','{$card_no}','{$state}','{$district}','{$village}','{$pin}','{$passport_photo}','{$card_photo}','{$phone}','{$email}','{$gender}','{$_SESSION ['sport_type']}','{$t_name}','{$date}')";
            $output=mysqli_query($con,$query);

            if($output){
            
            redirect("team-reg.php");
        }
    }


    $query="INSERT INTO `player_reg` (`age_category`, `player_name`, `gaurdian_name`, `doc_type`, `card_no`, `state`, `district`, `town`,`pincode`, `image`, `doc_img`, `phone`, `email`, `gender`, `sport`, `team_name`,`date`) VALUES ('{$_SESSION["age_cat"]}','{$p_name}','{$gaurdian}','{$document}','{$card_no}','{$state}','{$district}','{$village}','{$pin}','{$passport_photo}','{$card_photo}','{$phone}','{$email}','{$gender}','{$_SESSION ['sport_type']}','{$t_name}','{$date}')";
                $output=mysqli_query($con,$query);

                if($output){
                
                    echo json_encode(array('message' => 'User registered successfully!','status' => true));
            }

?>

The user is registered successfully but the image is not uploading to the server.

1 Answers

To upload an image using API, the most suitable method is to pass base64 encoded image data in Json request, then decode the base64 encoded string into file using file_put_contents().

$img_info = explode(',',$data['photo']);

$image_content = base64_decode($img_info[1]);

$img_extension = substr($img_info[0], 11, 3);

$img_filename = "images/passport_photo/some_random_text".$img_extension;

file_put_contents($img_filename, $image_content);
//apply your filters of size, extensions before uploading the image

But base64 is not a good idea to upload large size images. First of all Upload image and then pass the image URL in json request.

Related