L'immagine Caricare file su cloud Rackspace utilizzando PHP
Domanda
Sto cercando di caricare un file all'altro nuvola Rackspace utilizzando il seguente codice:
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);
?>
Ora sto ottenendo l'errore gollowing:
Fatal error: eccezione Uncaught 'BadContentTypeException' con il messaggio 'richiesta Content-Type non impostato' 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} gettato in C: \ xampp \ htdocs \ Rackspace \ cloudfiles.php on line 1645
Quindi, qualcuno ha qualche idea su questo? grazie in anticipo.
Soluzione
http://github.com/rackspace/php -cloudfiles / blob / master / cloudfiles.php alla funzione _guess_content_type () che sta cercando il Content-type e non è trovarlo. O avete bisogno di aggiungere ulteriori informazioni al vostro / share / magic o probabilmente è possibile impostare il Content-type prima di chiamare il load_from_filename se si sa cosa il tipo di contenuto è.
Altri suggerimenti
Ecco una correzione se si dispone di funzioni né MIME o FileInfo disponibili:
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;
}
Se non avete l'estensione FileInfo abilitato (abilitato per impostazione predefinita dal PHP 5,30). Vi suggerisco di controllare se mime_content_type () funzione è disponibile.
Sembra che se si dispone di nessuna di queste, il Content-Type non può essere rilevato. Se nessuno dei due è disponibile in questo momento, mi piacerebbe avere FileInfo
ho trovato la soluzione di Chris Bake utile. Avevo bisogno di mettere un "" di fronte a ogni estensione di sotto.
$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;
}
Si potrebbe anche voler considerare l'utilizzo della più recente Rackspace PHP SDK . Ecco il codice di esempio per creazione di un oggetto .