Bilderupload auf Rackspace Cloud-Dateien mit PHP
Frage
Ich versuche Datei auf Rackspace Cloud-Datei hochladen, um den folgenden 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);
?>
Jetzt bin ich die gollowing Störung zu erhalten:
Fatal error: abgefangene Ausnahme 'BadContentTypeException' mit der Meldung 'Required Content-Type nicht gesetzt' in C: \ xampp \ htdocs \ Höheneinheit \ cloudfiles.php: 1645-Stack trace: # 0 C: \ xampp \ htdocs \ Höheneinheit \ cloudfiles.php (1962): CF_Object -> _ guess_content_type ( 'C: \ xampp \ tmp \ ph ...') # 1 C: \ xampp \ htdocs \ Höheneinheit \ upload.php (24): CF_Object-> load_from_filename (‘ C: \ xampp \ tmp \ ph ... ') # 2 {main} in C geworfen: \ xampp \ htdocs \ Höheneinheit \ cloudfiles.php on line 1645
So jemand dies eine Ahnung haben? Vielen Dank im Voraus.
Lösung
Blick auf http://github.com/rackspace/php -cloudfiles / Blob / Master / cloudfiles.php an der Funktion _guess_content_type () sieht es für den Content-Typen und es findet es nicht. Entweder müssen Sie mehr Informationen zu Ihrem / share / magic hinzuzufügen, oder Sie können sich wahrscheinlich den Content-Typ vor dem Aufruf des load_from_filename einstellen, wenn Sie wissen, was die Art des Inhalts ist.
Andere Tipps
Hier ist ein Update, wenn Sie weder Mime oder Fileinfo-Funktionen zur Verfügung haben:
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;
}
Wenn Sie die Fileinfo-Erweiterung nicht haben aktiviert (standardmäßig aktiviert, da PHP 5,30). Ich schlage vor, Sie prüfen, ob mime_content_type () Funktion zur Verfügung.
Es scheint, dass, wenn Sie keiner von denen haben, kann der Content-Type nicht erkannt werden. Wenn weder jetzt verfügbar ist, würde ich bekommen Fileinfo
Ich fand Chris Bake-Lösung hilfreich. Ich brauchte ein setzen „“ vor jeder Verlängerung unten.
$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;
}
Sie können auch die neueren Beamten zu prüfen, mit PHP SDK Rackspace . Hier ist der Beispielcode für Erzeugen eines Objekts .