我试图使用下面的代码上传到文件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);

?>

现在我正的gollowing错误:

致命错误:未捕获的异常 'BadContentTypeException' 与消息中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 {主}扔在C:\ XAMPP \ htdocs中\ Rackspace公司\ cloudfiles.php上线1645

因此,任何一个对此有任何想法?由于事先。

有帮助吗?

解决方案

看着 http://github.com/rackspace/php -cloudfiles / BLOB /主/ cloudfiles.php 在功能_guess_content_type()这是要查找的内容类型,它不是找到它。要么你需要更多的信息添加到您的/股/魔法或者您也可以致电load_from_filename如果你知道的内容类型是什么之前可能设置的内容类型。

其他提示

下面是一个修复,如果你既没有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;
}

如果你没有启用FileInfo的扩展名(默认启用,因为PHP 5.30)。我建议你检查是否mime_content_type()函数是可用的。

看来,如果你既没有这些的,内容类型不能被检测到。如果没有可现在,我会得到的FileInfo

我发现克里斯烘烤的解决方案很有帮助。我需要把一个“”在下面的每个延伸的前面。

$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;
}

您可能还需要考虑使用较新的官方 Rackspace的PHP SDK 。下面是创建对象示例代码。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top