2017-04-21 130 views
0

我需要保存在服务器pdfs的文件夹中,并在数据库中保存相关url。 但与我告诉你下面的代码我得到的错误在PHP中:“please choose a file”...我不知道为什么。如何从POST客户端使用POST方法获取参数到php

我告诉你我的代码:

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

我希望你能帮帮我!

+1

这个'[” nome_pdf']'无效。使用'tmp_name' – Akintunde007

+0

另外你的脚本的SQL注入风险很高上。访问http://bobby-tables.com并了解准备好的语句并使用它们,否则您的数据库可能会在几秒钟内被黑客入侵。你也应该在ID字段上使用自动增量。 – Twinfriends

回答

0

你的错误来自这里

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

注:使用文件上传PHP打交道时,不要使用$_POST。使用$_FILES。我想你感到困惑与文件的名称,也与该$_FILESarray

nome_pdf相关的array_keys不是$_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类型来检查扩展

+0

我试过但我得到了同样的错误 – alex

+0

再试一次。更新后的代码 – Akintunde007

+0

只是再次进行了更改。现在尝试 – Akintunde007

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'])){ 
+0

不起作用.... – alex

+0

在表格中您使用的是enctype =“multipart/form-data”

+0

我不使用html,但邮递员 – alex

相关问题