2017-04-21 13 views
0

私はサーバ上のフォルダに保存する必要があります。pdfsとdbには相対URLがあります。 しかし、以下のコードを表示すると、PHPでエラーが表示されます。 "please choose a file" ...理由はわかりません。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にアクセスし、プリペアドステートメントについて学び、それらを使用してください。そうしないと、DBが数秒でハッキングされる可能性があります。また、あなたのIDフィールドで自動インクリメントを使用する必要があります。 – Twinfriends

答えて

0

あなたのエラーは、ここでは

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

NOTEから来ている:PHPでファイルのアップロードを扱うときは、$_POSTを使用しないでください。 $_FILESを使用してください。私はあなたが$_FILESarray

nome_pdfが$_FILES配列ではないに関連付けられているファイルの名前ともarray_keysと混同していると思います。ダンプをvar_dump($_FILES['pdf']);にしようとすると、nome_pdfが配列に含まれていないことがわかります。 tmp_nameが空でないことを確認します

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

上記の変更:

はと行を交換してください。 はまた、ファイルを移動する前に、私は

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

があるべき拡張

+0

私は試みましたが、同じエラーが発生します – alex

+0

もう一度お試しください。更新されたコード – Akintunde007

+0

がもう一度変更を加えました。今すぐお試しください – Akintunde007

0

このラインをチェックするためにMIMEタイプを使用することをお勧め

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

関連する問題