使用rackspace上传图像

时间:2012-04-18 16:37:27

标签: php image html5 rackspace

所以我有以下代码将图像上传到dir / uploads。

<?php

// If you want to ignore the uploaded files, 
// set $demo_mode to true;

$demo_mode = false;
$upload_dir = 'uploads/';
$allowed_ext = array('jpg','jpeg','png','gif');


if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    exit_status('Error! Wrong HTTP method!');
}


if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){

    $pic = $_FILES['pic'];

    if(!in_array(get_extension($pic['name']),$allowed_ext)){
        exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
    }   

    if($demo_mode){ 
        // File uploads are ignored. We only log them.

        $line = implode('', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
        file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);

        exit_status('Uploads are ignored in demo mode.');
    }


    // Move the uploaded file from the temporary 
    // directory to the uploads folder:

    if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){
        exit_status('File was uploaded successfuly!');
    }
}

exit_status('Something went wrong with your upload!');


// Helper functions

function exit_status($str){
    echo json_encode(array('status'=>$str));
    exit;
}

function get_extension($file_name){
    $ext = explode('.', $file_name);
    $ext = array_pop($ext);
    return strtolower($ext);
}
?>

我试着改变一下代码。因此,我希望它上传到我的rackspace,而不是上传到目录。我使用以下代码(适用于我的其他文件上传)     

// 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('ContainerName');

// store file information
$localfile = $_FILES['pic']['tmp_name'];
$filename = $_FILES['pic']['name'];

// upload file to Rackspace
$object = $container->create_object($filename);
$object->load_from_filename($localfile);
?>

我将图片上传的部分替换为dir / upload到上面的部分。它不起作用。

我正在使用这个HTML5拖放下载上传。

http://tutorialzine.com/2011/09/html5-file-upload-jquery-php/

请帮助。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。我遇到的问题是该对象没有与之关联的文件类型。有人可能会更好地解释它,因为我是一个PHP新手,但这是我用它来为我工作:

$object = $container->create_object($filename);
$object->content_type = "image/jpeg"; //This is where the issue is
$object->load_from_filename($localfile);
相关问题