使用 PHP 将图像上传到rackspace云文件

发布于 2024-08-22 06:37:52 字数 1377 浏览 8 评论 0原文

我正在尝试使用以下代码将文件上传到机架空间云文件:

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”,并在 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 on line 1645

所以有人对此有任何想法吗?提前致谢。

I am trying to upload file to rackspace cloud file using the following code:

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);

?>

Now i am getting the gollowing error:

Fatal error: Uncaught exception 'BadContentTypeException' with message 'Required Content-Type not set' in C:\xampp\htdocs\rackspace\cloudfiles.php:1645 Stack trace: #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} thrown in C:\xampp\htdocs\rackspace\cloudfiles.php on line 1645

So any one have any idea about this? thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

输什么也不输骨气 2024-08-29 06:37:52

查看 http://github.com/rackspace/php-cloudfiles/ blob/master/cloudfiles.php 在函数 _guess_content_type() 中它正在寻找内容类型,但没有找到它。您需要向 /share/magic 添加更多信息,或者如果您知道内容类型是什么,则可以在调用 load_from_filename 之前设置内容类型。

looking at http://github.com/rackspace/php-cloudfiles/blob/master/cloudfiles.php at the function _guess_content_type() it's looking for the Content-type and it's not finding it. Either you need to add more information to your /share/magic or you can probably set the Content-type before calling the load_from_filename if you know what the content type is.

停顿的约定 2024-08-29 06:37:52

如果您没有 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;
}

Here's a fix if you have neither mime or FileInfo functions available:

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;
}
泪眸﹌ 2024-08-29 06:37:52

如果您没有启用 FileInfo 扩展(自 PHP 5.30 起默认启用)。我建议您检查 mime_content_type() 函数是否可用。

看来如果你没有这些,则无法检测到 Content-Type。如果现在两者都不可用,我会得到 FileInfo

If you dont have the FileInfo extension enabled (enabled by default since PHP 5.30). I suggest you check if mime_content_type() function is available.

It seems that if you have neither of those, the Content-Type cannot be detected. If neither is available right now, i'd get FileInfo

沒落の蓅哖 2024-08-29 06:37:52

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

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

I found Chris Bake's solution helpful. I needed to put a "." in front of each extension below.

$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;
}
请止步禁区 2024-08-29 06:37:52

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

You might also want to consider using the newer official Rackspace PHP SDK. Here's the sample code for creating an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文