当我在Codeigniter中上传图像时,其名称将保存到数据库中,但不会保存在默认文件夹中。我正在使用Ajax提交图片
我的观点"简档
<div id="timelineBackground">
<?php
{
$image_properties = array('src' => base_url("uploads/" . $timeline_image),'width' => '1000px','height'=> '400px','id'=>'coverimg', 'title' => 'That was quite a night','rel' => 'lightbox');
//echo img($image_properties);
?>
<div id="timelineselector">
<?php echo form_open_multipart('',["id"=>"form_cover"]); ?>
<input type="hidden" name="email" value="<?php echo $email ;?>" >
<?php echo form_upload(["name"=>"timelineimage"]); ?>
<?php echo form_submit(["name"=>"submit","value"=>"Submit"]); ?>
<?php echo form_close(); ?>
</div><?php
}
?></div>
这是我的ajax代码
jQuery('#form_cover').submit(function(e){
e.preventDefault();
var formData = new FormData(this);
var url= '<?php echo base_url("user/coverimage"); ?>';
formData.value
jQuery.ajax({
type: "POST",
url:url,
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data)
{
$('#coverimg').attr('src',data);
},
error: function(data){
//error function
}
});
});
我的控制器&#34;用户&#34;
public function coverimage(){
$config = [
'upload_path' => './uploads/',
'allowed_types' => 'jpg|gif|png|jpeg',
'max_size' => 10000000000000,
'max_width' => 1024000000,
'max_height' => 7680000000,
];
$this->load->library('upload', $config);
if(!$this->upload->do_upload('timelineimage') ) {
$post = $this->input->post();
unset($post['submit']);
$upload_data = $this->upload->data();
$file_name=$_FILES['timelineimage'];
$this->load->model('Pmodel');
$this->Pmodel->timeline_upload_model($post,$file_name);
$image_properties = array(
'src' => base_url("./uploads/" . $file_name['name']),
'width' => '200px',
'height'=> '200px',
'title' => 'That was quite a night',
'rel' => 'lightbox'
);
// echo img($image_properties);
echo base_url("uploads/" . $file_name['name']);
exit();
}else {
$upload_error = $this->upload->display_errors();
$this->load->view('admin/add_article',compact('upload_error'));
}
}
我的模特&#34; Pmodel&#34;
public function timeline_upload_model($arr,$arra)
{
$email=$arr['email'];
$image=$arra['name'];
$data=array('timelineimage'=>$image);
$query=$this->db->where('email',$email)->update('user_data',$data);
return $query;
}
为什么它只显示名称而不显示图像?
答案 0 :(得分:0)
我找到了答案并发布了代码,万一其他人想看看这个......
'view'
<div id="timelineBackground">
<?php
{
$image_properties = array('src' => base_url("uploads/" . $timeline_image),'width' => '1000px','height'=> '400px','id'=>'coverimg', 'title' => 'That was quite a night','rel' => 'lightbox');
echo img($image_properties);
?>
<div id="timelineselector">
<?php echo form_open_multipart('user/coverimage',["id"=>"form_cover"]); ?>
<input type="hidden" name="id" value="<?php echo $id ;?>" >
<?php echo form_upload(["name"=>"timelineimage"]); ?>
<?php echo form_submit(["name"=>"submit","value"=>"Submit"]); ?>
<?php echo form_close(); ?>
</div><?php
}
?></div>
'ajax'
jQuery('#form_cover').submit(function(e){
e.preventDefault();
var formData = new FormData(this);
var url= '<?php echo base_url("user/coverimage"); ?>';
formData.value
jQuery.ajax({
type: "POST",
url:url,
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data)
{
$('#coverimg').attr('src',data);
},
error: function(data){
//error function
}
});
});
和控制器:
public function coverimage()
{
print_r($_POST);
print_r($_FILES);
$config = [
'upload_path' => './uploads/',
'allowed_types' => 'jpg|gif|png|jpeg',
'max_size' => 10000000000000,
'max_width' => 1024000000,
'max_height' => 7680000000,
];
$this->load->library('upload', $config);
$this->upload->initialize($config);
$timelineimage="timelineimage";
if(!$this->upload->do_upload($timelineimage))
{
$upload_error = $this->upload->display_errors();
$this->load->view('dashboard/profile',compact('upload_error'));
}
else
{
$post = $this->input->post();
unset($post['submit']);
//print_r($post);
$upload_data = $this->upload->data();
//print_r($upload_data);
$file_name=$_FILES['timelineimage'];
$this->load->model('Pmodel');
$this->Pmodel->timeline_upload_model($post,$file_name);
$image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']);
//echo base_url("uploads/" . $file_name['name']);
}
}