Welcome Guest, Not a member yet? Register   Sign In
function in controller not working. Could be a problem with URL?

Hello all,

I'm having trouble with codeigniter. I only downloaded it yesterday and have been working my way through the tutorials.

I'm having some problems with my controllers. I've tried the basic 'blog' tutorial controller which works fine but when I try either the 'upload' or 'form validation' tutorials the controller does not recognise any of the functions, for example, when I try the upload tutorial, when I upload a file it doesn't recognise the 'do_upload' function within my upload.php file.

I get this error message:

The requested URL /Codeigniter/upload/do_upload was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Here's the code:

class Upload extends CI_Controller {

function __construct()
  $this->load->helper(array('form', 'url'));

function index()
  $this->load->view('upload_form', array('error' => ' ' ));

function do_upload()
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

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

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

   $this->load->view('upload_form', $error);
   $data = array('upload_data' => $this->upload->data());

   $this->load->view('upload_success', $data);

And in my 'upload_form.php' page inside my 'views' folder is this code:

<?php echo $error;?>

  echo form_open_multipart('upload/do_upload');?>

  <input type="file" name="userfile" size="20" />
  <br /><br />
  &lt;input type="submit" value="upload" /&gt;

&lt;?php echo form_close(); ?&gt;
My root folder is; 'http://localhost/Codeigniter/index.php/upload/' this works fine but when I press the upload button the url changes to: 'http://localhost/Codeigniter/upload/do_upload'. This may be the problem but I'm new to Codeigniter so can anyone help? Thanks in advance.

config base_url in application/config/config.php

config['base_url'] = 'http://localhost/site/index.php/';

It's ok I've managed to sort the problem. It was my base url in my config file.

I've changed;

$config['base_url'] = 'http://localhost/Codeigniter/';


$config['base_url'] = 'http://localhost/Codeigniter/index.php/';

and it works fine now. Thanks anyway hot_sauce.

base_url should not contain index.php. That's what the "index_page" setting is for.

Yes that is wrong.
Keep the base url the same but change this line in the config file:

$config['index_page'] = 'index.php';

As you gain experience you can create a .htaccess to remove the index.php from the url. But I would just run with this as creating a .htaccess can be confusing as some webhosts need to override the main php ini() settings.

Theme © iAndrew 2016 - Forum software by © MyBB