簡體   English   中英

如何創建 PHP rest api 從任何設備上傳文件?

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

我正在創建一個 API 來注冊用戶。 我可以成功注冊,但圖片沒有上傳。 我在谷歌上進行了一些搜索並獲得了一些關於 file_put_contet() 的信息,但不知道實現它的正確方法

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

?>

用戶注冊成功,但圖片沒有上傳到服務器。

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

但是 base64 不是上傳大尺寸圖片的好主意。 首先上傳圖片,然后在 json 請求中傳遞圖片 URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM