Welcome Guest, Not a member yet? Register   Sign In
simple upload
#1

[eluser]zyklon[/eluser]
Can someone tell me why this simple piece of code don't work?It's a form with an image upload.I made a function for the image upload,it's working separately but when i make
$this->upload_photos(); doesn't work.

function upload_photos()



$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';

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

if ( ! $this->upload->do_upload())
{
}
else
{
$upload_data = $this->upload->data();
$post_image = $upload_data["file_name"];

return $post_image ;
}



public function add_sponsors()
{
$view_data["action"] = "add_sponsors";
$this->load->helper(array('form', 'url'));



$this->load->model('Admin_model','admin');


$view_data['internal_error']="";

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

$rules["name"] = "trim|required";
$rules["description"] = "trim|required";
$rules["website"] = "trim";


$this->validation->set_rules($rules);

$fields["name"] = "Nume";
$fields["description"] = "Descriere";
$fields["website"] = "Website(without http://)";

$this->validation->set_fields($fields);

$this->validation->set_message("required","Nu ati completat campul %s");


$this->validation->set_error_delimiters('<br>', '');

if($this->validation->run() == FALSE)
{

$this->load->view('admin/front/admin_front_view', $view_data);

}

else
{



$result_sponsors = $this->admin->check_sponsors("sponsors",$this->input->post('name','TRUE'));
if($result_sponsors == TRUE)
{

$name = $this->input->post('name','TRUE');
$description = $this->input->post('description','TRUE');
$website = $this->input->post('website','TRUE');


$post_image = $this->upload_photos();

$result_create_sponsors = $this->admin->create_sponsors("sponsors",$name,$description,$website,$post_image);
if($result_create_sponsors == TRUE)
{
header("Location: ". $this->config->item('base_url')."index.php/admin/view_sponsors");
}
else

{
$view_data['internal_error'] = 'Inregistrarea nu a putut fi facuta';
$this->load->view('admin/front/admin_front_view', $view_data);
}

}

else
{
$view_data['internal_error']='Acest sponsor deja exista';
$this->load->view('admin/front/admin_front_view', $view_data);


}
}

}

The error i get is:

Column 'file' cannot be null

INSERT INTO gl_sponsors (id, name, description, website, file) VALUES ('', 'fdgf', 'dfgdsg', '', NULL)
#2

[eluser]gtech[/eluser]
an educated guess.. I think the problem is in the upload_photos function

your postdata is missing when you call it from a fucntion as its not passed from a form.. the upload expects the variable 'userfile' in the post variables. As this is missing your filename is set to NULL.



this bit from the documentation should explain it:
-------------------------------------

$this->upload->do_upload()

Performs the upload based on the preferences you've set. Note: By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart type:

&lt;form method="post" action="some_action" enctype="multipart/form-data" /&gt;

If you would like to set your own field name simply pass its value to the do_upload function:
$field_name = "some_field_name";
$this->upload->do_upload($field_name)

-----------------------------------
hope that helps




Theme © iAndrew 2016 - Forum software by © MyBB