تحميل الصور إلى ملفات السحابة Rackspace باستخدام PHP

StackOverflow https://stackoverflow.com/questions/2303190

  •  21-09-2019
  •  | 
  •  

سؤال

أحاول تحميل الملف إلى ملف Cloud Rackspace باستخدام الرمز التالي:

تحميل. 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>

expload.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 (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

إذن أي شخص لديه أي فكرة عن هذا؟ شكرا لك مقدما.

هل كانت مفيدة؟

المحلول

انظر الى http://github.com/rackspace/php-cloudfiles/blob/master/cloudfiles.php في الدالة _guess_content_type () ، تبحث عن نوع المحتوى ولا يجد ذلك. إما أن تحتاج إلى إضافة المزيد من المعلومات إلى /مشاركة /سحر أو ربما يمكنك تعيين نوع المحتوى قبل الاتصال بـ load_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 () متاحة.

يبدو أنه إذا لم يكن لديك أي من هؤلاء ، فلا يمكن اكتشاف نوع المحتوى. إذا لم يكن أي منهما متاحًا الآن ، فسأحصل معلومات الملف

لقد وجدت حل كريس بيك مفيدًا. كنت بحاجة لوضع ". أمام كل امتداد أدناه.

$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