我正在尝试将文件上传到我的Amazon S3存储桶。我的朋友在过去的网站上已经实现了这一点,但一年后它出了问题,我无法弄清楚问题出在哪里。我们正在使用由Geoff gaudreault开发的S3 PHP库。
对我来说,这个库看起来非常简单,实际上,它似乎只有一个关键函数:putObject()
。
不幸的是,我甚至还没有开始就遇到了以下错误消息:Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
这是我的Codeigniter PHP代码,用于我的上传表单action
:
function s3(){
$config['upload_path'] = $_SERVER["DOCUMENT_ROOT"].'/temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['max_width'] = '1024000';
$config['max_height'] = '768000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
echo 'failure';
}
else
{
$data = array('upload_data' => $this->upload->data());
$fn = $data['file_name'];
$type = substr($fn, strrpos($fn, '.') + 1);
$this->load->library('s3');
$temp_file_path = $_SERVER["DOCUMENT_ROOT"]."/temp/" . $data['file_name'];
$contents = read_file($temp_file_path); // will this blow up or timeout for large files?!
$newFileName = uniqid().".".substr($temp_file_path, strrpos($temp_file_path, '.') + 1);
$contentPath = "mysite.com/Images";
$this->s3->putObject($newFileName, $contents, $contentPath, 'private', $type);
echo 'success';
}
}
有人有想法吗?
encrypt_name
选项,而不是手动生成$newFileName
。其次,$data['full_path']
包含上传文件的完整路径,$data['file_ext']
包含文件扩展名,最后,您应该使用$config['upload_path'] = './temp';
而不是使用$_SERVER['DOCUMENT_ROOT']
。上传脚本采用相对于根目录中index.php的路径。希望对您有所帮助。 - Gavin$this->load->library('s3');
以及对该库的putObject()
调用时,问题才会出现。 - tim peterson