【问题标题】:why is redirect() not allowing me to finish my function call?为什么 redirect() 不允许我完成函数调用?
【发布时间】:2011-09-12 07:49:53
【问题描述】:

所以我正在编写一个函数,允许我将一个数据库表中的 ID 添加到另一个数据库表中以将两者关联起来,但我遇到了一些麻烦,似乎无法弄清楚该怎么做。任何帮助都会非常感激。

请原谅我的一些乱七八糟的代码,我一直在尝试许多似乎不起作用的不同解决方案。

class photo_modle extends CI_Model {

var $gallery_path;
var $image_name;
var $row;
var $gid; // gallary ID
var $iid; // image ID
function photo_modle() {
ob_start();
    $this->gallery_path = realpath(APPPATH . '../images');
}

function uploadPhoto() {


    $config = array(
        'allowed_types' => 'jpg|jpeg',
        'upload_path' => $this->gallery_path,
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $this->image_name = $image_data['file_name'];
    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => realpath($this->gallery_path . '/thumb/normal/'),
        'width' => 248,
        'height' => 198,
        'maintain_ratio' => false
    );

    $this->load->library('image_lib', $config);
    $this->image_lib->resize();

    $data = array(
        'image_name' => $this->image_name,
        'description' => $this->input->post('description'),
        'name' => $this->input->post('name')

    );



    $str = $this->db->insert_string('images', $data);

    $this->db->query($str);
    $this->iid = $this->db->insert_id();


    $grayscale_path = '/Applications/XAMPP/xamppfiles/htdocs/images/thumb/normal/' . $this->image_name;
    header('Content-type: image/jpeg');
    $img = imagecreatefromjpeg($grayscale_path);
    imagefilter($img, IMG_FILTER_GRAYSCALE);
    imagejpeg($img, '/Applications/XAMPP/xamppfiles/htdocs/images/thumb/rollover/' . $this->image_name, 100);
    imagedestroy($img);
    $ndata = array (
        'image_name' => $this->image_name,
        'description' => $this->input->post('description'),
        'name' => $this->input->post('name'),
        'id' => $this->iid
         );

    $this->session->set_userdata($ndata);


}




function add_new_gallery() {


    $ndata = array(
        'gallery_name' => $this->input->post('gallery_name'),
        'description' => $this->input->post('gallery_description'),
     ); 

     $n_str = $this->db->insert('gallery', $ndata);
    // this is the only place where i can put the redirect without it returning errors
    // but if i do it here it does not pass back the gid variable which i need. 
    // I also should mention that I have a  header('Content-type: image/jpeg'); above all of
    // this, and that is why I have to do a redirect, so that I don't get an error. and that 
    // header code is nessasary, for I am doing some photo manipulation that requires it. 
     redirect('site/uploaded'); 
     $this->db->query($n_str);
     $this->gid = $this->db->insert_id();

     // I was trying to send that info in the session, but even that did not work because of the 
     // redirect
     $sdata = array(
         'gallery_id' => $this->gid
     );

     $this->session->set_userdata($sdata);


}
// this function needs the info that is not getting passed. 
function addId() {
      $sdata = array('gallery_id' => $this->session->userdata('gallery_id'));
     $where = "id = ".$this->session->userdata('image_id'); 
     $str = $this->db->update_string('images', $sdata, $where);

     $this->db->query($str);
}

感谢任何可以提供帮助的人,非常感谢您抽出宝贵的时间。任何建议都会很棒!

【问题讨论】:

  • 您遇到的错误是什么?
  • this is the only place where i can put the redirect without it returning errors - 为什么?我假设这个函数发送标题并终止执行,所以它应该放在所有其他操作之后。
  • 图片“localhost/index.php/site/photoUploader”包含错误,无法显示。
  • @AlexW.H.B.请回答我的问题,为什么不能在函数末尾调用“重定向”。并向我们​​展示您的“redirect()”函数的代码。
  • 这意味着只有在不发送 redirect 标头时才应发送“Content-type”标头。尝试在脚本开头调用ob_start();

标签: php http function redirect header


【解决方案1】:

如果您只是处理图像并将结果保存到文件中,则无需发送Content-type: image/jpeg 标头。

在您向浏览器发送图像时才需要。

您可能对imagejpeg 的工作原理感到困惑,因为它具有双重行为。 imagejpg 可以:

  • 如果您不传递$filename,则直接将图像输出到浏览器。当然,您需要警告浏览器期待图像数据,即在您向其发送内容类型标头时。

  • 将图像保存到文件中,如果你给它一个$filename

在您的情况下,您确实将其传递给$filename,以便将图像保存到磁盘。但是您还发送了一个Content-type: image/jpeg,因此您的浏览器期望接收图像数据,这永远不会发生,然后它“无法显示,因为它包含错误”。

【讨论】:

  • 嘿,非常感谢您的回答!我不知道。我刚刚删除了该内容类型,它工作正常。我不得不重定向到另一个页面,但现在我可以让它提交给它自己。谢谢你。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2011-04-26
  • 2023-03-27
相关资源
最近更新 更多