【问题标题】:Upload an image to OpenFire via PHP API service通过 PHP API 服务将图像上传到 OpenFire
【发布时间】:2012-12-22 19:21:40
【问题描述】:

我正在构建一个处理图像上传的 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>

【问题讨论】:

  • 澄清:move_uploaded_file 的问题在这里并不重要,更多的是“如何将图像文件格式化为字符串”,可以在 xml 数据包中使用..

标签: php xml image xmpp openfire


【解决方案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...

它只是一个文件......仅仅因为你已经移动了它并不意味着你不能再为其他事情触摸它。

【讨论】:

    【解决方案2】:

    所以我在使用 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;
        }
    }
    

    希望它可以帮助尝试做同样事情的人!

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多