2016-03-09 84 views
1

我正在通過PHP文件從Android應用程序發送base64字符串到服務器。
我得到這個從視頻教程:
如何將base64圖像發送到服務器 - PHP

<?php 
    require 'Init.php'; 
    header('Content-type : bitmap; charset=utf8'); 

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

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

     $decoded_string = base64_decode($encoded_string); 

     $path = 'ProfileIcons/' .$image_name; 

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

     $is_written = fwrite($file, $decoded_string); 
     fclose($file); 
    } 
?> 

它存儲在目錄中的圖片,但是當你打開,它沒有形象。這是一個空白的PNG。代碼有問題嗎?如果是這樣,我應該使用什麼?謝謝。

+0

你能發表由 - $ encoded_string返回的字符串格式嗎?檢查你是否需要操縱它 – Inducesmile

回答

2

有幾個點,你應該檢查:

1.確保服務器收到的base64字符串

$ encoded_string = $ _ POST [ 'encoded_string'];

檢查長度爲$encoded_string,它應該與android客戶端說的長度相同。

  • 確保解碼工作

    $ decoded_string = BASE64_DECODE($ encoded_string);

  • 檢查的$decoded_string的長度,它不應該是零或一些奇怪的。

  • 確保文件的文字作品

    $ is_written =的fwrite($文件,$ decoded_string);

  • $is_written應該是已經寫入文件中的數據的長度,如果是false,那麼什麼是錯的。

    +0

    有些事情肯定是錯誤的。因爲即使我手動更改了代碼並設置了硬編碼值,圖像仍然是空白的。 – Cas

    +0

    在哪裏?你可以編輯問題並告訴我們你做了什麼嗎? – free6om

    +0

    我意識到它在工作。出於某種原因,當我下載圖像時,它變爲空白。但我不打算下載它,所以沒關係。但很高興知道我應該檢查我的代碼的事情,我是PHP的新手。謝謝。 – Cas

    相關問題