1

我正在构建一个处理图像上传的 PHP API。使用 $_FILES 和 PHP move_uploaded_file() 将图像正常上传到 apache 服务器 - 这会将文件保存在本地,现在可用于网站。在同一过程中,我还需要访问 XMPP 服务器 (OpenFire) 以使用相同的图像更新头像。

我无法弄清楚如何解决这个问题,我想使用 $_FILES['userfile']['tmp_name'] 中的文件引用从 PHP tmp/ 位置获取实际的图像文件数据 - 这就是我们的方式当我们使用 move_uploaded_file() 时访问它。运行 move_uploaded_file() 后 tmp 文件数据是否仍然可用?名称表明并非如此,今天的实验显示之前的资源,在运行 move_uploaded_file() 之后是错误的。那么我如何才能点击图像数据并将其格式化以在 xml 数据包中使用它,同时仍然为后续 move_uploaded_file() 保留原始 tmp 文件?- 请参阅下面的 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),它就不再在临时目录中了。这就是为什么它是MOVE而不是COPY。没有什么说你不能移动它,然后从它被移动到的任何地方使用 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 回答