2013-01-08 84 views
1

我正在構建一個處理圖像上傳的PHP API。使用$ _FILES和PHP move_uploaded_file()將圖像正常上傳到apache服務器 - 這將該文件保存在本地,現在可用於網站。在同一過程中,我還需要使用相同的圖像來訪問XMPP服務器(OpenFire)以更新頭像。通過PHP API服務將圖像上傳到OpenFire

我無法弄清楚如何處理這個問題,我想使用$ _FILES ['userfile'] ['tmp_name']中的文件引用從PHP tmp/location抓取實際的圖像文件數據 - 這是我們在使用move_uploaded_file()時如何訪問它。運行move_uploaded_file()後,tmp文件數據是否仍然可用?這個名字不會提示,實驗今天會顯示一個資源,在運行move_uploaded_file()之後會顯示false。那麼我怎樣才能打開圖像數據並將其格式化以便在xml數據包中使用它,同時仍然保留原始tmp文件以保留後續move_uploaded_file()? - 請參閱下面的XML示例。

另一種方法是使用get_file_contents()並讀取我們剛剛保存到文件系統的文件 - 但這看起來很愚蠢。

接下來的一點是我的頭腦。OpenFire XMPP服務器允許通過XMPP接口本身分配。有關如何上傳和設置用戶頭像的示例,請參見http://xmpp.org/extensions/xep-0084.html#examples-multiple

有人請點我朝着正確的方向,我需要發送一個XML(XMPP)數據包,以明火服務器 - 我已經XMPPHP運行,並連接到服務器無後顧之憂,在XML中的格式那給了我悲傷。

例XMP包從http://xmpp.org/extensions/xep-0084.html#examples-multiple(例1發佈虛擬形象數據,數據節點)

<iq type='set' from='[email protected]/chamber' id='publish1'> 
<pubsub xmlns='http://jabber.org/protocol/pubsub'> 
<publish node='urn:xmpp:avatar:data'> 
    <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'> 
    <data xmlns='urn:xmpp:avatar:data'> 
     qANQR1DBwU4DX7jmYZnncm... 
    </data> 
    </item> 
</publish> 

+0

澄清:move_uploaded_file的問題在這裏並不那麼重要,它的更多「我怎樣才能將一個圖像文件格式化爲一個字符串」,它可以在xml包中使用。 – Nick

回答

-1

號一旦使用move_uploaded_file),它不再是臨時目錄。這就是爲什麼它是移動而不是複製。沒有什麼說你不能移動它,然後從它移動到的任何地方使用copy()操作,例如

move_uploaded_file($_FILES['file']['tmp_name'], '/site/file1.txt'); 
copy('/site/file1.txt', '/size/other/dir/file2.txt'); 
etc... 

它只是一個文件...只是因爲你已經移動它並不意味着你不能再次觸摸它的其他東西。

1

所以我得到這個xmpp頭像問題使用xmpphp庫工作。我寫了這個功能來啓動類:

/* 
* arg 1: $file_handle = the local path to the image 
* arg 2: $file_type = the file extension, jpg, png etc. 
*/ 

function upload_new_xmpp_avatar($file_handle, $file_type) 
{ 
// XMPPHP class. Used to connect to xmpp server 
require_once "xmpphp/XMPPHP/XMPP.php; 

$xmpp_host = "xmpp.domain.com"; 

$xmpp_server = ""xmpp.domain.com"; 

$xmpp_port = "5222"; 

$admin_username = "admin_user"; 

$admin_pasword = "admin_pw"; 

$conn = new XMPPHP_XMPP($xmpp_host, $port, $admin_username, $admin_password, "xmpphp", $xmpp_server, $printlog = false, $loglevel = XMPPHP_Log::LEVEL_VERBOSE); 

try 
{ 
    // load the image from filesystem 
    $raw_file = file_get_contents($file_handle);                    

    // get the image information array, width, height etc. 
    $image_info = getimagesize($file_handle);                    

    // build sha1 hash of the raw image data 
    $image_sha1_hash = sha1($file_handle);                     

    // base64 encode it! 
    $file_data = base64_encode($raw_file);                     

    $conn->connect(); 

    $conn->processUntil('session_start'); 

    $conn->upload_new_avatar_from_file($user_r->user_name, $file_data, $image_info, $image_sha1_hash); 

    $conn->disconnect(); 
} 
catch(XMPPHP_Exception $e) 
{ 
    api_die("xmpp_error: ".$e->getMessage()); 
} 

return $image_sha1_hash; 

}

此外,還需要一類新的功能添加到XMPP.php類:

// adding upload avatar option 

public function upload_new_avatar_from_file($user_name, $file_data, $image_info, $image_sha1_hash)   // 08 01 2013 NM adding upload new avatar function 
{ 
    $id = 'upload_avatar_file_' . $this->getID(); 

    $image_file_size = ($image_info['bits']*8);                // convert bits to bytes.. dur. 

    $image_mime_type = $image_info['mime']; 

    $image_height = $image_info[1];                   

    $image_width = $image_info[0]; 

    $xml = "<iq type='set' to='[email protected]$xmpp_server' id='$id'> 
       <vCard xmlns='vcard-temp'> 
        <PHOTO xmlns='vcard-temp'> 
         <BINVAL xmlns='vcard-temp'>$file_data</BINVAL> 
         <TYPE xmlns='vcard-temp'>$image_mime_type</TYPE> 
        </PHOTO> 
       </vCard> 
      </iq>"; 

    // 2nd xml comand sets the uploaded image as the new vCard avatar 

    $xml2 = "<presence> 
       <priority>30</priority> 
       <status>Online</status> 
       <x xmlns='vcard-temp:x:update'> 
        <photo>$image_sha1_hash</photo> 
       </x> 
       <x xmlns='jabber:x:avatar'> 
        <hash>$image_sha1_hash</hash> 
       </x> 
       <c xmlns='http://jabber.org/protocol/caps' node='http://vacuum-im.googlecode.com' ver='nvOfScxvX/KRll5e2pqmMEBIls0=' hash='sha-1'/> 
       </presence>"; 

    // end vacuum vCard example 

    // http://xmpp.org/extensions/xep-0084.html node and metadata example  

    $this->addIdHandler($id, 'upload_avatar_handler'); 

    // send the 1st packet 
    $this->send($xml);                       

    $this->addIdHandler($id2, 'upload_avatar_handler'); 

    // send the 2nd packet 
    $this->send($xml2);                       
} 

protected function upload_avatar_handler($xml) 
{ 
    switch ($xml->attrs['type']) 
    { 
     case 'error': 
      $this->event('upload_new_avatar', 'error'); 
     break; 

     default: 
      $this->event('upload_new_avatar', 'default'); 
     break; 
    } 
} 

希望它可以幫助別人嘗試做相同!

相關問題