1

画像のアップロードを処理する PHP API を構築しています。画像は通常、$_FILES と PHP move_uploaded_file() を使用して Apache サーバーにアップロードされます。これにより、ファイルがローカルに保存され、Web サイトで利用できるようになります。また、同じプロセス中に同じイメージを使用してアバターを更新するために、XMPP サーバー (OpenFire) にアクセスする必要があります。

$_FILES['userfile']['tmp_name'] にあるファイル参照を使用して、PHP tmp/ の場所から実際の画像ファイル データを取得したいと思います。 move_uploaded_file() を使用するときにアクセスします。move_uploaded_file() を実行した後、tmp ファイルのデータは引き続き利用できますか? 名前はそうではないことを示唆しており、今日の実験では、move_uploaded_file() を実行する前にリソースが明らかになり、実行後に false が明らかになりました。では、次の move_uploaded_file() のために元の tmp ファイルをそのまま残しながら、画像データをヒットして xml パケットで使用するためにフォーマットするにはどうすればよいでしょうか? - xml の例については、以下を参照してください。

別の方法は、 get_file_contents() を使用して、ファイルシステムに保存したばかりのファイルを読み取ることですが、それはばかげているようです。

次のビットは私の頭の中です。OpenFire XMPP サーバーは、XMPP インターフェイス自体を介して割り当てを許可します。ユーザーのアバターをアップロードして設定する方法の例については、http: //xmpp.org/extensions/xep-0084.html#examples-multipleを参照してください。

誰かがここで私を正しい方向に向けてください.XML(XMPP)パケットを直火サーバーに送信する必要があります-私はすでにXMPPHPを実行しており、心配することなくサーバーに接続しています.xmlのフォーマットは私に悲しみを与えています. .

http://xmpp.org/extensions/xep-0084.html#examples-multipleからの XMP パケットの例(例 1. データ ノードへのアバター データの公開)

<iq type='set' from='juliet@capulet.lit/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>

4

2 に答える 2

1

そのため、xmpphp ライブラリを使用して、この xmpp アバターの問題が発生しました。クラスを開始するためにこの関数を書きました:

/*
 *  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='$user_name@$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;
    }
}

同じことをしようとしている人に役立つことを願っています!

于 2013-01-16T13:03:29.303 に答える
-1

いいえ。move_uploaded_file を使用すると、一時ディレクトリにはなくなります。そのため、COPYではなくMOVEです。移動できないとは何も言っておらず、後で移動先から copy() 操作を使用します。

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

それはただのファイルです...移動したからといって、他のことのために二度と触れられないわけではありません。

于 2013-01-08T20:33:45.170 に答える