2016-10-26 14 views
0

AndroidからlocalhostサーバーにBase64形式の文字列ファイルとして画像を送信しています。 Google ChromeのPostman拡張機能でテストしています。ここでPHPでbase64でエンコードされた画像文字列を受け取ってファイルに書き込む

が私のコードです:

<?php 
header('Content-type : bitmap; charset=utf-8'); 

if(isset($_POST["encoded_string"])){ 

    $encoded_string = $_POST["encoded_string"]; 
    $image_name = $_POST["image_name"]; 

    $decoded_string = base64_decode($encoded_string); 

    $path = 'android_connect/images/'.$image_name; 

    $file = fopen($path, 'wb'); 

    $is_written = fwrite($file, $decoded_string); 
    fclose($file); 

    if($is_written > 0) { 

     $connection = mysqli_connect('localhost', 'root', 'rifat20081995','phpmyadmin'); 
     $query = "INSERT INTO Pictures(Name,Path) values('$image_name','$path');"; 

     $result = mysqli_query($connection, $query) ; 

     if($result){ 
      echo "success"; 
     }else{ 
      echo "failed"; 
     } 

     mysqli_close($connection); 
    } 
    else 
    { 
     echo "file not written"; 
    } 
} 
?> 

私はこのようなコードを使用していた多くの例を見ましたが、私にとっては、「ファイルが書き込まれていない」毎回言います。

私はUbuntuにいます。私は書き込み中にいくつかの許可の問題があると思います。しかし、私はすべての権限を設定しましたが、何も変わりませんでした。

+0

は、実際にデータを取得するあなたのポストですか?あなたの '$ encoded_string'を返し、送信しているデータがサーバが受信しているのと同じデータであるかどうか確認してください。次に、ファイルがサーバーに書き込まれているかどうかをデバッグしてみてください。 – Pazuzu156

+0

はいBase64のフォーマットされた文字列を取得し、文字列をデコードし、サーバーに画像を書き込んでいます... –

+0

http応答ではどうなりますか? – Clay

答えて

0

解決しました。問題はいくつかのアクセス許可の問題でした。私もfwrite()file_put_contents($dir . '/' . $image_name, $decoded_string)と置き換えました。

全コード:

<?php 
header('Content-type : bitmap; charset=utf-8'); 

if(isset($_POST["encoded_string"])){ 


    $dir = 'images'; 
    if(!file_exists($dir)) 
    { 
      $oldmask = umask(0);// helpful when used in linux server 
      mkdir ($dir, 0744); 
    } 

    $encoded_string = $_POST["encoded_string"]; 
    $image_name = $_POST["image_name"]; 
    $decoded_string = base64_decode($encoded_string); 

    file_put_contents ($dir.'/'.$image_name, $decoded_string); 
    $path=$dir.'/'.$image_name; 

     $connection = mysqli_connect('localhost', 'root', 'rifat20081995','phpmyadmin'); 
     $query = "INSERT INTO Pictures(Name,Path) values('$image_name','$path');"; 

     $result = mysqli_query($connection, $query) ; 

     if($result){ 
      echo "success"; 
     }else{ 
      echo "failed"; 
     } 

     mysqli_close($connection); 
    } 
    else 
    { 
     echo "file not written"; 
    } 

?> 
+0

上/下の三角形の下のチェックマークを使ってあなたの答えを受け入れることを忘れないでください。あなたがやる)。 – Clay

+0

@ClaytonSmithありがとうございました... –

関連する問題