2010-02-20 70 views
3

我嘗試使用下面的代碼上傳文件Rackspace的雲文件Rackspace的雲文件:圖片上傳到使用PHP

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中有消息'Required Content-Type not set':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

那麼任何人對此有什麼想法?提前致謝。

回答

5

看着http://github.com/rackspace/php-cloudfiles/blob/master/cloudfiles.php在函數_guess_content_type()它正在尋找內容類型,它沒有找到它。要麼你需要向/ share/magic添加更多信息,要麼你可以在調用load_from_filename之前設置Content-type,如果你知道內容類型是什麼的話。

+0

謝謝,我知道了..發佈後的問題。無論如何,謝謝 – 2010-02-20 19:21:14

1

如果您沒有啓用FileInfo擴展(自PHP 5.30起默認啓用)。我建議你檢查一下mime_content_type()函數是否可用。

看起來,如果您沒有這些內容,則無法檢測到Content-Type。如果沒有可現在,我會得到FileInfo

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; 
} 
+0

良好的答覆,解決了我的問題! – 2010-07-31 00:01:32

+0

優秀的解決方案 – Giri 2011-08-23 10:59:51

+0

完美。讓我頭疼! – Ignas 2012-12-10 18:21:06

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