PHPを使用してRackspaceのクラウドファイルへの画像のアップロード
質問
私は次のコードを使用して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 \ペーハー...')#1 C:\ xamppの\ htdocsに\ Rackspaceの\ upload.php(24):CF_Object-> load_from_filename(」 C:\ XAMPP \ TMP \のpH ... ')#2 {メイン} Cでスロー:\ XAMPP \ htdocsに\ラックスペース\ cloudfiles.phpライン1645上の
いずれかが、このことについてどんな考えを持っているので?事前に感謝します。
解決
http://github.com/rackspace/phpを見て-cloudfiles /ブロブ/マスター/ cloudfiles.php の機能_guess_content_typeで()それはコンテンツタイプを探していると、それはそれを見つけていません。どちらかあなたは/共有/魔法に多くの情報を追加する必要があるか、あなたはおそらく、あなたがコンテンツタイプが何であるかを知っていればload_from_filenameを呼び出す前に、コンテンツタイプを設定することができます。
他のヒント
はここで修正です。
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;
}
(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 。ここでがオブジェクトを作成するためのサンプルコードです。