我在Codeigniter中遇到照片上传系统问题。
我在我的控制器中注册时上传并调整照片大小,一旦完成,我会加载一个“上传成功”视图,我想再次显示照片。
问题是,
和
这是控制器..检查靠近底部的注释,因为顶部基本上只是调整大小的逻辑和(虽然它现在有点难看)它确实有效
public function do_upload()
{
$config['upload_path'] = './upload_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1200';
$config['max_height'] = '768';
$_maxResizedWidth = '242';
$this->load->library('upload', $config);
if ( $this->upload->do_upload() )
{
$this->data['upload_data'] = $this->upload->data();
$this->data['title'] = "Upload Successful";
switch($this->data['upload_data']['file_type']){
case "image/jpg":
$_file_suffix = ".jpg";
break;
case "image/jpeg":
$_file_suffix = ".jpg";
break;
case "image/png":
$_file_suffix = ".png";
break;
case "image/gif":
$_file_suffix = ".gif";
break;
case "image/pjpeg":
$_file_suffix = ".jpg";
break;
}
$_fileName = $this->data['upload_data']['file_name'];
$_oldUploadPath = $config['upload_path'] . $_fileName;
$_random_key = strtotime(date('Y-m-d H:i:s'));
$_newUploadPath = $config['upload_path'] . 'resize_' . $_random_key . $_file_suffix;
rename( $_oldUploadPath, $_newUploadPath );
$_oldSize = getimagesize($_newUploadPath);
$_oldWidth = $_oldSize[0];
if($_oldWidth > $_maxResizedWidth){
$_scale = $_maxResizedWidth/$_oldWidth;
$_oldHeight = $_oldSize[1];
$_imageType = image_type_to_mime_type($_oldSize[2]);
$_newWidth = $_oldWidth * $_scale;
$_newHeight = $_oldHeight * $_scale;
$_resizedImage = imagecreatetruecolor($_newWidth, $_newHeight);
switch($_imageType) {
case "image/gif":
$_source=imagecreatefromgif($_newUploadPath);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$_source=imagecreatefromjpeg($_newUploadPath);
break;
case "image/png":
case "image/x-png":
$_source=imagecreatefrompng($_newUploadPath);
break;
}
imagecopyresampled($_resizedImage, $_source, 0, 0, 0, 0, $_newWidth, $_newHeight, $_oldWidth, $_oldHeight);
switch($_imageType) {
case "image/gif":
imagegif($_resizedImage,$_newUploadPath);
break;
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
imagejpeg($_resizedImage,$_newUploadPath,90);
break;
case "image/png":
case "image/x-png":
imagepng($_resizedImage,$_newUploadPath);
break;
}
}
$_newUploadPathWithoutDot = substr($_newUploadPath,1);
$this->load->model('Member_model');
$ID = $this->the_user->id;
/// ****************************************************************************
/// I step into this next if loop even though the upload_photo method does work.
/// In my view I get the "there was a problem" .. but the DB has been correctly updated
if(!$this->Member_model->upload_photo( $ID, $_newUploadPathWithoutDot));
{
$uploadData = $this->upload->data();
$this->data['message'] = "There was a problem entering your photo in the database ==";
};
/// And this is the view that's called
$this->load->view('upload_big_success_view', $this->data);
}
else
{
$this->data['message'] = "there was a problem " . $this->upload->display_errors() . dirname(__FILE__);
$this->data['title'] = "Upload Unsuccessful";
$this->load->view('profile_photo_view', $this->data);
}
}
(我知道那里有很多杂乱,我会给它一个重构)。
当视图首次加载时,几乎看起来图像是“未就绪”,即使一切都有效 - 上传,调整大小,文件路径都输入到数据库中......没有任何错误会引发任何错误到最后一个循环,它似乎认为“ - > upload_photo()”失败了。然后当我第一次打电话给我的视图时..
<img src"<?php echo $the_user->large_image_location; ?>" >
..不产生输出。但经过简单的刷新后,确实如此。虽然视图中仍然显示“有问题消息”。
顺便说一句 - 这是模特。没什么好看的。
public function upload_photo($ID, $_newUploadPath)
{
$sql = "UPDATE users SET large_image_location=? WHERE id=?";
$query = $this->db->query($sql, array($_newUploadPath, $ID));
return $query;
}
所有想法?
答案 0 :(得分:1)
Codeigniter在加载视图时不会刷新页面,所以它永远不会加载图像,而是需要告诉Codeigniter使用redirect()
函数刷新页面。您应该重定向到成功页面,而不是在do_upload()
方法的末尾加载成功视图。
我会创建一个upload_big_success()
方法,为您加载upload_big_success_view
,然后重定向到该方法。
替换do_upload()
中的以下行:
$this->load->view('upload_big_success_view', $this->data);
使用:
redirect(upload_big_success, 'location');
然后在同一个控制器中为您的成功页面创建一个方法。
public function upload_big_success() {
// And this is the view that's called
$this->load->view('upload_big_success_view');
}