使用PHP上传图片到rackspace云文件

3
我将尝试使用以下代码上传文件到rackspace云文件:

Upload.html

<form action="upload.php" enctype="multipart/form-data" method="POST">
    File: 
    <input name="upload" type="file" /> 
    <input name="submit" type="submit" value="Upload To Rackspace!" />
</form>

Upload.php

<?php

// include the API
require('cloudfiles.php');

// cloud info
$username = ""; // username
$key = ""; // api key

// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);

// Get the container we want to use
$container = $conn->get_container('resumetune');

// store file information
$localfile = $_FILES['upload']['tmp_name'];
$filename  = $_FILES['upload']['name'];

// upload file to Rackspace
$object = $container->create_object($filename);
$object->load_from_filename($localfile);

?>

我现在遇到如下错误:

致命错误:未捕获的异常'BadContentTypeException',信息为'Required Content-Type not set',位于C:\ xampp \ htdocs \ rackspace \ cloudfiles.php的第1645行。堆栈跟踪:#0 C:\ xampp \ htdocs \ rackspace \ cloudfiles.php(1962):CF_Object -> _guess_content_type('C:\ xampp \ tmp \ ph ...')#1 C:\ xampp \ htdocs \ rackspace \ upload.php(24):CF_Object -> load_from_filename('C:\ xampp \ tmp \ ph ...')#2 {main}抛出于C:\ xampp \ htdocs \ rackspace \ cloudfiles.php的1645行。

请问有人知道这是什么问题吗?提前感谢。

5个回答

5
如果您没有可用的mime或FileInfo函数,这里有一个解决方法:
function _guess_content_type($handle) {

    $ext = ".".end(explode(".", $handle));
    switch($ext)
    {
        case 'jpg': $this->content_type = "image/jpeg"; break;
        case 'gif': $this->content_type = "image/gif"; break;
        case 'png': $this->content_type = "image/png"; break;
        default: $this->content_type = "image/jpeg"; break;
    }

    if ($this->content_type)
        return;

    if (function_exists("finfo_open")) {
        $local_magic = dirname(__FILE__) . "/share/magic";
        $finfo = @finfo_open(FILEINFO_MIME, $local_magic);

        if (!$finfo) 
            $finfo = @finfo_open(FILEINFO_MIME);

        if ($finfo) {

            if (is_file((string)$handle))
                $ct = @finfo_file($finfo, $handle);
            else 
                $ct = @finfo_buffer($finfo, $handle);

            /* PHP 5.3 fileinfo display extra information like
               charset so we remove everything after the ; since
               we are not into that stuff */
            if ($ct) {
                $extra_content_type_info = strpos($ct, "; ");
                if ($extra_content_type_info)
                    $ct = substr($ct, 0, $extra_content_type_info);
            }

            if ($ct && $ct != 'application/octet-stream')
                $this->content_type = $ct;

            @finfo_close($finfo);
        }
    }

    if (!$this->content_type && (string)is_file($handle) && function_exists("mime_content_type")) {
        $this->content_type = mime_content_type($handle);
    }

    if (!$this->content_type) {
        throw new BadContentTypeException("Required Content-Type not set");
    }
    return True;
}

5

谢谢,我已经明白了...刚发布问题后就明白了。无论如何谢谢。 - M.M.H.Masud

1
如果您没有启用FileInfo扩展(自PHP 5.30以来默认启用),我建议您检查mime_content_type()函数是否可用。
看起来如果您两者都没有,Content-Type无法被检测到。如果现在都不可用,我会获取FileInfo

0

我发现克里斯·贝克的解决方案很有帮助。我需要在下面的每个扩展名前加上“.”

$ext = ".".end(explode(".", $handle));
switch($ext)
{
    case '.jpg': $this->content_type = "image/jpeg"; break;
    case '.gif': $this->content_type = "image/gif"; break;
    case '.png': $this->content_type = "image/png"; break;
    default: $this->content_type = "image/jpeg"; break;
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接