如何使用POST方法从rest客户端获取参数到php

时间:2017-04-21 08:30:24

标签: php rest

我需要保存在服务器pdf上的文件夹中,并在db中保存相对URL。 但是我在下面给你看的代码我在php中得到了错误:“please choose a file”......我不知道为什么。

我告诉你我的代码:

REST客户端: enter image description here

服务器端:

<?php
//importing dbDetails file
require_once 'dbDetails.php';

//this is our upload folder
$upload_path = 'uploads/';

//Getting the server ip
$server_ip = gethostbyname(gethostname());



//creating the upload url
//$upload_url = 'http://'.$server_ip.'/AndroidImageUpload/'.$upload_path;
$upload_url = 'http://'.$server_ip.'/azz/'.$upload_path;


//response array
$response = array();


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

    //checking the required parameters from the request
    if( isset($_POST['nome_pdf'])  && isset($_FILES['pdf']['nome_pdf']) && isset($_POST['id'])){

        //connecting to the database
        $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');

        //getting name from the request
        $nome = $_POST['nome_pdf'];



        //getting file info from the request
        $fileinfo = pathinfo($_FILES['pdf']['nome_pdf']);




        //getting the file extension
        $extension = $fileinfo['extension'];

        $id = $_POST['id'];


        //file url to store in the database
        $file_url = $upload_url . getFileName() . '.' . $extension;


        //file path to upload in the server
        $file_path = $upload_path . getFileName() . '.'. $extension;

        //trying to save the file in the directory
        try{
            //saving the file
            move_uploaded_file($_FILES['pdf']['tmp_name'],$file_path);

            $sql = "INSERT INTO `my_db`.`pdfss` (`id_pdf`, `nome_pdf`, `url_pdf`,`id_user`) VALUES (NULL, '$nome','$file_url', '$id');";

            //adding the path and name to database
            if(mysqli_query($con,$sql)){

                //filling response array with values
                $response['error'] = false;
               //  $response['url'] = $file_url;
//                 $response['name'] = $name;
                 $response['nome_pdf'] = $nome;
                 $response['url_pdf'] = $file_url;
            }
            //if some error occurred
        }catch(Exception $e){
            $response['error']=true;
            $response['message']=$e->getMessage();
        } 
        //closing the connection
        mysqli_close($con);
    }else{
        $response['error']=true;
        $response['message']='Please choose a file';
    }

    //displaying the response
    echo json_encode($response);
}

我希望你能帮助我!

2 个答案:

答案 0 :(得分:0)

您的错误来自此处

 isset($_FILES['pdf']['nome_pdf'])

注意:在PHP中处理文件上载时,不要使用$_POST。使用$_FILES。我认为您对该文件的名称以及与array_keys $_FILES

相关联的array感到困惑

nome_pdf不在$_FILES数组中。如果您尝试转储var_dump($_FILES['pdf']);,您会看到nome_pdf不在数组中。

将该行替换为:

if( isset($_FILES['pdf'])  && $_FILES['pdf']['tmp_name'] != '' && isset($_POST['id'])){
#add your code
else{
$response['error']=true;
  $response['message']= 'PDF FILE: '. $_FILES['pdf'].' ID of post: '.$_POST['id'];
   exit;
}
echo(json_encode($reponse));

上述更改将确保tmp_name不为空。 在移动文件之前,我建议使用mime类型来检查扩展名

答案 1 :(得分:0)

这一行

if( isset($_POST['nome_pdf'])  && isset($_FILES['pdf']['nome_pdf']) && isset($_POST['id'])){

应该是

if( isset($_POST['nome_pdf'])  && isset($_FILES['nome_pdf']['tmp_name']) && isset($_POST['id'])){

或者

if( isset($_POST['nome_pdf'])  && isset($_FILES['nome_pdf']['name']) && isset($_POST['id'])){