2012-02-01 61 views
0

我用補品(用於休息的PHP庫)製作一個休息Web服務。REST Webservice HTTP_PUT參數

我使用根據CRUD和REST編輯一個元素。

所以我打電話給我的方法與圖片和文件類型,並解析參數,並保存在我的服務器上的base64編碼文件。

代碼:

function put($request) { 

    $response = new Response($request); 
    $msg = new ErrorMessage(); 
    $dbmodel = new DBModel(); 
    $arr = array('Data' => null,'Message' =>null,'Code' => null); 
    try{ 
     $split = explode ('&',$request->data); 
     $para = array(); 

     foreach($split as $i) { 
      $names = explode('=',$i); 
      if(!isset($names[0]) or !isset($names[1])) 
      { 
       throw new Exception(); 
      } 
      $para[$names[0]] = $names[1]; 
     } 
    } 
    catch(Exception $e) 
    { 
     $arr['Code'] = 400; 
     $arr['Message'] = $msg->getMessage(400); 
     $response->body = json_encode($arr); 
     return $response; 
    } 


    if (isset($para['picture']) or isset($para['filetype'])) 
    { 
     if (isset($para['picture']) and isset($para['filetype'])) 
     { 
      if (!($para['filetype'] == 'jpg' || $para['filetype'] == 'png')) 
      { 
       $arr['Code'] = 688; 
       $arr['Message'] = $msg->getMessage(617); 
       $response->body = json_encode($arr); 
       return $response; 
      } 
      $bin = base64_decode($para['picture']); 
      if (strlen($bin) >524288) 
      { 
       $arr['Code'] = 617; 
       $arr['Message'] = $msg->getMessage(617); 
       $response->body = json_encode($arr); 
       return $response; 
      } 

      $uid = $dbmodel->getUid($sid); 
      if($uid<1) 
      { 
       $arr['Code'] = 699; 
       $arr['Message'] = $msg->getMessage(699); 
       $response->body = json_encode($arr); 
       return $response; 

      } 
      $file = fopen($_SERVER['DOCUMENT_ROOT']."/img/".$uid.".".$para['filetype'], 'wb'); 
      fwrite($file, $bin); 
      fclose($file); 

     } 
     else 
     { 
      $arr['Code'] = 616; 
      $arr['Message'] = $msg->getMessage(616); 
      $response->body = json_encode($arr); 
      return $response; 
     } 

    } 

    $arr['Code'] = 200; 
    $arr['Message'] = $msg->getMessage(200); 
    $response->body = json_encode($arr); 
    return $response; 
} 

問題:保存的圖片是不是像原來它不能顯示圖像

我用http://www.redio.info/werkzeuge/file2base64.html我的圖片轉換成的base64。我認爲這個問題可能出現在我的代碼開始處的解析中。

原文:13.872字節

新形象:14.313字節

+0

是不是像原來的那樣...怎麼樣? – 2012-02-01 20:47:27

+0

我更新了我的任務 – user547995 2012-02-01 20:52:34

+0

用hexeditor(甚至只是一個文本編輯器)查看這兩個文件,並尋找明顯的差異。 – 2012-02-01 20:55:06

回答

1

您的圖片參數會大概進行了urlencoded,這可以解釋更大的文件大小。 (例如'/'到%2F)

嘗試在解碼之前在參數上加上urldecode。

$bin = base64_decode(urldecode($para['picture']));