Welcome Guest, Not a member yet? Register   Sign In
Tutorial File Upload Class Problem
#21

[eluser]TheFuzzy0ne[/eluser]
You've answer 0/4 of the questions I asked. Smile

Which tutorial are you following?
#22

[eluser]davehedgehog[/eluser]
haha, sorry

Are any of your controller methods working? index, create, edit_blog and so on?

Yes there all working besides the edit.

What’s the URL you’re using to access the page you want?

I access the create for excample at-

www.###.com/blog/create

and the edit view is in the same folder

oh so the way im trying to access it is

www.###.com/blog/edit

Where is you controller file, and what it’s called?

the controller file is in the controllers and called blog.php

and Im following the CodeIgniter User Guide Version 2.1.3 and have read everything some several times starting to get everything slowly, although proberly slightly confused lol.
#23

[eluser]TheFuzzy0ne[/eluser]
I would expect a 404 in that case, you're calling www.###.com/blog/edit and your method is called edit_blog(). Rename it to edit() and you should find it works now. Smile
#24

[eluser]davehedgehog[/eluser]
aha lol it doesnt work but least the page shows cheer =)
#25

[eluser]davehedgehog[/eluser]
Uh wonder if I could pick your brain again....trying to basically mix the upload and blog tutorials to save the url in the database and image into the folder, it seems to work, as in no errors. but donesnt save the path or the image:/

heres the controller code:-
Code:
<?php
class Blog extends CI_Controller{

public function __construct()
{
  parent::__construct();
  $this->load->model('blog_model');
}

public function index()
{
  $data['blog'] = $this->blog_model->get_blog();
  $data['title'] = 'blog archive';

  $this->load->view('templates/header', $data);
  $this->load->view('blog/index', $data);
  $this->load->view('templates/footer');
}

public function view($slug)
{
  $data['blog_item']=$this->blog_model->get_blog($slug);

  if(empty($data['blog_item']))
  {
   show_404();
  }

  $data['title']=$data['blog_item']['title'];

  $this->load->view('templates/header',$data);
  $this->load->view('blog/view',$data);
  $this->load->view('templates/footer',$data);
}

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

  $this->load->helper(array('form', 'url'));
  $this->load->library('upload', $config);
  $this->load->library('form_validation');

  $data['title']='Create blog item';

  $this->form_validation->set_rules('title','Title','required');
  $this->form_validation->set_rules('post','Post','required');
  $this->form_validation->set_rules('category','Category','required');

  if($this->form_validation->run() === FALSE)
  {
   $this->load->view('templates/header',$data);
   $this->load->view('blog/create');
   $this->load->view('templates/footer');
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   $data = $this->upload->data();

   $filepath = $data['full_path'];

   $data = array(
       'image_path' => $filepath
   );

   $this->db->insert('image', $data);
   $this->blog_model->set_blog();
   $this->load->view('blog/success');
  }
}
}
?>
#26

[eluser]Aken[/eluser]
You aren't actually running do_upload() anywhere.
#27

[eluser]davehedgehog[/eluser]
so how do i do that? :/
#28

[eluser]TheFuzzy0ne[/eluser]
I normally set it up as a validation callback, so that if there are any errors from the upload library, you can display them through the form validation library. You'll need to remember to delete the uploaded file if there are any validation errors.

It may make more sense to allow files to be upload after the other data has been submitted and added to the database. If someone is uploading a large file, it can get annoying having to wait for 2 minutes, only to be told there's an error, and then to having to select a file to upload again.
#29

[eluser]davehedgehog[/eluser]
ok so this is what Ive done....

Code:
public function create()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '2048';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

  $this->load->helper(array('form', 'url'));
  $this->load->library('upload', $config);

  $this->load->library('form_validation');

  $data['title']='Create blog item';

  $this->form_validation->set_rules('title','Title','required');
  $this->form_validation->set_rules('post','Post','required');
  $this->form_validation->set_rules('category','Category','required');
  $this->form_validation->set_rules('image', 'Image', 'callback__do_upload');

  if($this->form_validation->run() === FALSE)
  {
   $this->load->view('templates/header',$data);
   $this->load->view('blog/create');
   $this->load->view('templates/footer');
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   $data = $this->upload->do_upload($data);
   $filepath = $data['full_path'];

   $data = array(
       'image_path' => $filepath
   );

           if ( ! $this->upload)
           {
             $error = array('error' => $this->upload->display_errors());
           }
    
   $this->db->insert('image', $data);
   $this->blog_model->set_blog();
   $this->load->view('blog/success');
  }
}
}

and it returns this message


A PHP Error was encountered

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: libraries/Upload.php

Line Number: 147

? so whats going wrong ? :/
#30

[eluser]TheFuzzy0ne[/eluser]
Please post your validation callback.

By default, the upload library expects your file field name to be "userfile". If it's anything else, you need to tell it:
Code:
$this->upload->do_upload('image');

There is a problem with this code:
Code:
// $this->upload->upload_data() should only be called AFTER you've called $this->upload->do_upload()
$data = array('upload_data' => $this->upload->data());

// This would be better off handled in your validation callback.
$data = $this->upload->do_upload($data);
Also, this line is not necessary:
Code:
if ( ! $this->upload) // Will only execute if the upload library is not loaded.
{
    // We should set the error message from within our validation method.
    $error = array('error' => $this->upload->display_errors());
}




Theme © iAndrew 2016 - Forum software by © MyBB