views:

59

answers:

2

I'm still very new to codeigniter. The issue i'm having is that the file uploads fine and it writes to the database without issue but it just doesn't return me to the upload form. Instead it stays in the do_upload and doesn't display anything. Even more bizarrely there is some source code behind the scenes that appears to be the message that comes up when you don't select an image to upload. Can someone tell my what it is i'm doing wrong because I want to be returning to my upload form after submission. Thanks in advance. Below is my code:

Controller:

function do_upload()
{
    if(($error = $this->Upload_model->do_upload()) === true)
    {

        $this->load->view('home/upload_form');

    }else{

        $this->load->view('home/upload_success', $error); 

    }

}

Model:

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2000';

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        return $error;
    }   
    else
    {
        $data = $this->upload->data();
        $full_path = 'uploads/' . $data['file_name'];

        $spam = array(
            'image_url' => $full_path,
            'url' => $this->input->post('url')
        );

        $id = $this->input->post('id');

        $this->db->where('id', $id);
        $this->db->update('NavItemData', $spam);

        return true;

    }
}

View (called upload_form):

<html>
<head>
<title>Upload Form</title>
</head>
<body>



<?php if(isset($buttons)) : foreach($buttons as $row) : ?>


<h2><?php echo $row->image_url; ?></h2>
<p><?php echo $row->url; ?></p>
<p><?php echo $row->name; ?></p>
<p><?php echo anchor("upload/update_nav/$row->id", 'edit'); ?></p>


<?php endforeach; ?>
<?php endif; ?>

</body>
</html>

Here is the actual upload form:

<html>
<head>
<title>Upload Form</title>
</head>
<body>



<?php echo form_open_multipart('upload/do_upload');?>
<?php if(isset($buttons)) : foreach($buttons as $row) : ?>
<h2><?php echo $row->name; ?></h2>
<input type="file" name="userfile" size="20" />
<input type="hidden" name="oldfile" value="<?php echo $row->image_url; ?>" />
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<br /><br />
<label>Url: </label><input type="text" name="url" value="<?php echo $row->url; ?>" /><br /><br />
<input type="submit" value="submit" />

</form>

<?php endforeach; ?>
<?php endif; ?>

</body>
</html>
A: 

There are some really confusing parts in your source code (like why is the view called upload form when it does not contain an upload form).

I see one problem in your controller and that is that the conditional will always execute the first branch. So you might want to change it to:

if(($error = $this->Upload_model->do_upload()) === true)

This will capture the error and execute the second branch on error. If this doesn't help, try also posting your upload form.

Jakub Hampl
as i say i'm still really new to ci and naming files logically has never been my strong point! they tend to have an organic evolution.thanks for the answer but alas i still get no joy.
Drew
+1  A: 

Have you tried the redirect() function?

redirect('upload');

Cheers,
Will

WillDonohoe
cheers dude, that's done the trick!
Drew