2

我在使用 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;  
}

那么有什么想法吗?

4

1 回答 1

1

Codeigniter 在加载视图时不会刷新页面,因此它永远不会加载图像,而是需要使用该redirect()函数告诉 Codeigniter 刷新页面。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');
}
于 2012-12-10T03:51:22.477 回答