Updating of my record is not working - php

I am almost close to complete my project but I have two issues which may be unable to fix please can anyone guide me, I really need help. The issue I am facing is that when I try to update "first name" or "last name" then I am getting an error, and the image path saved in the database is getting deleted. But if I try to update my image then it`s working fine. The second issue is that when I try to update my image the old image is not getting replaced with the new one, even the old image is avaliable in the folder. How to solve this two issues please I really need help .
From.php (controller part)
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Form_model->displayrecordsById($id);
$this->form_validation->set_rules('fname', 'First name', 'required');
$this->form_validation->set_rules('lname', 'Last name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('update_records',$result);
} else {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$fn=$this->input->post('fname');
$ln=$this->input->post('lname');
$un=$this->input->post('username');
$em=$this->input->post('email');
if($this->upload->do_upload('filename'))
{
$fi= $this->upload->data('file_name');
$unlink = unlink('uploads/' . $row->filename);
} else {
$fi= $result['data']->filename;
}
$this->Form_model->updaterecords($fn,$ln,$un,$em,$fi,$id);
echo 'Successfully updated your record';
//exit();
}
}

The first obvious issue is that $row var doesn't exist. In context it should be $result['data']->filename. And for your first question I don't see how updating fname and lname would logically lead to the previously uploaded file getting deleted unless another image is uploaded as do_upload will fail if no file is uploaded and thus enter your else statement.
Here I rearranged some things and added encrypt_filename. The primary purpose of this is to avoid issues where a user upload an image with the same name as a previously uploaded image even though the images might be different.
public function updatedata() {
$id = $this->input->get('id');
if (is_null($id)) {
show_error('Id cannot be empty');
}
$result['data'] = $this->Form_model->displayrecordsById($id);
$this->form_validation->set_rules('fname', 'First name', 'required');
$this->form_validation->set_rules('lname', 'Last name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('update_records', $result);
} else {
$config['encrypt_name'] = true; // highly recommended
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$fi = $result['data']->filename;
if ($this->upload->do_upload('filename')) {
if (is_file('./uploads/' . $fi)) {
#unlink('./uploads/' . $fi);
}
$fi = $this->upload->data('file_name'); // set var to new name
}
$fn = $this->input->post('fname');
$ln = $this->input->post('lname');
$un = $this->input->post('username');
$em = $this->input->post('email');
$this->Form_model->updaterecords($fn, $ln, $un, $em, $fi, $id);
echo 'Successfully updated your record';
}
}

Related

How can I store validation errors related to image upload as flash massages in this Codeigniter 3 application?

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
The posts, of course, have main images. There is a default image if no image is uploaded by the user.
There are restrictions on image files a user can upload:
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
I have managed to display the upload error messages that correspond to the configuration above (for your curiosity and/or use, the code is here).
For the update() method however, since there are redirects involved, all form validation messages are stored as "flash messages".
public function update() {
// Form data validation rules
$this->form_validation->set_rules('title', 'Title', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('desc', 'Short description', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('body', 'Body', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
$id = $this->input->post('id');
// Update slug (from title)
if ($this->form_validation->run()) {
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, $id);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
} else {
$slug = $this->input->post('slug');
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
// Use name field in do_upload method
if (!$this->upload->do_upload('userfile')) {
$errors = array('error' => $this->upload->display_errors());
// Dysplay upload validation errors
// only if a file is uploaded and there are errors
if (empty($_FILES['userfile']['name'])) {
$errors = [];
}
if (!empty($errors)) {
$data['upload_errors'] = $errors;
}
} else {
$data = $this->upload->data();
$post_image = $data[ 'raw_name'].$data[ 'file_ext'];
}
}
else {
$post_image = $this->input->post('postimage');
}
if ($this->form_validation->run() && empty($errors)) {
$this->Posts_model->update_post($id, $post_image, $slug);
$this->session->set_flashdata('post_updated', 'Your post has been updated');
redirect('/' . $slug);
} else {
$this->form_validation->run();
$this->session->set_flashdata('errors', validation_errors());
// this line was added later
// but the bug persists
$this->session->set_flashdata('upload_errors', $errors);
redirect('/dashboard/posts/edit/' . $slug);
}
}
In the edit-post.php view I have:
<input type="hidden" name="postimage" id="postimage" value="<?php echo $post->post_image; ?>">
<label for="postimage">Upload an image</label>
<div class="form-group">
<input type="file" name="userfile" id="postimage" size="20">
<?php if ($this->session->flashdata('upload_errors')) { ?>
<div class="error-messages">
<?php if(isset($upload_errors)){
foreach ($upload_errors as $upload_error) {
echo $upload_error;
}
}?>
</div>
<?php } ?>
</div>
I have not been able to add the image upload errors messages as "flash messages".
How can I do that?
In edit-post.php you have to change:
<?php if ($this->session->flashdata('upload_errors')) { ?>
With:
<?php if ($upload_errors = $this->session->flashdata('upload_errors')) { ?>
Try this:
$this->session->set_flashdata('errors', validation_errors());
$this->session->set_flashdata('upload_errors', $errors);
Or you can create an $errorArray from validation_errors() and $errors.
I do the exact same thing (upload errors go into a flashdata element). Here's how it works for me:
(I'll skip the upload config... but for this example, it's stored in an array conveniently named $config)
In my controller:
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$this->session->set_flashdata('message', "An error ocurred: ".$this->upload->display_errors());
$this->session->set_flashdata('alert_class', "alert-danger");
}
else
{
$this->session->set_flashdata('message', "Upload successful");
$this->session->set_flashdata('alert_class', "alert-success");
}
In my view (all my views carry this code)
<?php
if (null !== $this->session->flashdata('message'))
{ ?>
<div class="alert <?php echo $this->session->flashdata('alert_class'); ?> text-center mb-2"><i class="fas fa-exclamation-triangle fa-fw"></i> <?php echo $this->session->flashdata('message'); ?></div>
<?php
}
?>
In this way, if I don't set a flashdata element called message the view won't even display the DIV where alert is displayed. If, on the other hand, I do set the message element, a col-12 alert is displayed with the message I defined and styled with the alert class I want (success, warning, danger, info, etc)
This will only work for upload validation errors. In your multipart form you may also have regular validation errors which you should check for separately. You can do the same with a flash element:
$this->session->set_flashdata('message', validation_errors());
I use the same element name (message) because I'd stop upon the first error, So there would be no upload error and form validation error in the same display.
This should work.
public function update()
{
// Form data validation rules
$this->form_validation->set_rules('title', 'Title', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('desc', 'Short description', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('body', 'Body', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
$id = $this->input->post('id');
$form_is_valid = $this->form_validation->run()
$errors = [];
// Update slug (from title)
if ($form_is_valid) {
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, $id);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
} else {
$slug = $this->input->post('slug');
$errors[] = validation_errors();
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
// Use name field in do_upload method
if (!$this->upload->do_upload('userfile')) {
$upload_errors = array('error' => $this->upload->display_errors());
// Dysplay upload validation errors
// only if a file is uploaded and there are errors
if (empty($_FILES['userfile']['name'])) {
$upload_errors = [];
}
if (!empty($upload_errors)) {
$data['upload_errors'] = $upload_errors;
$errors[] = $upload_errors;
}
} else {
$data = $this->upload->data();
$post_image = $data[ 'raw_name'].$data[ 'file_ext'];
}
}
else {
$post_image = $this->input->post('postimage');
}
if (empty($errors)) {
$this->Posts_model->update_post($id, $post_image, $slug);
$this->session->set_flashdata('post_updated', 'Your post has been updated');
redirect('/' . $slug);
} else {
$this->session->set_flashdata('errors', $errors);
redirect('/dashboard/posts/edit/' . $slug);
}
}
$this->form_vlaidation->run() is called only once.
Errors are saved both (form validation & upload) in the same array.
Post is updated only if $errors is empty.
You should think how you can improve the code further.
Try this one .Add this in constructor
$this->session->keep_flashdata('errors');
and remove session data after use it in view
$this->session->unset_userdata('errors');

Codeigniter : I am trying to submit form either if user chooses any image to upload or not choose. is it possible?

Right now, submit form works good when i am choosing image to upload . But i am trying to submit same form even if not choosing any image...
controller
function create(){
$this->data['categories'] = $this->News_model->getNewsCategories();
$this->data['status'] = $this->status;
if($this->input->post()){
$title =$this->input->post('title');
$slug =$this->input->post('slug');
$desc =$this->input->post('desc');
$category_id =$this->input->post('category');
$upload_path = FCPATH.'assets/uploads/news';
$field_name ='image';
$temp_file_name = $this->file[$field_name]['name'];
$file_name = time().'_'.$this->randomString(10).'.'.$this->getFileExtension($temp_file_name);
if(!$this->uploadImage($upload_path,$file_name,$field_name)){
$this->session->set_flashdata('error', $this->file_error);
}
else{
$this->createThumbnail($this->file_data['full_path'], $upload_path.'/thumb/'.$file_name,75,50);
if(!$this->News_model->insertNews($title,$slug,$desc,$category_id,$file_name)){
$this->session->set_flashdata('error', 'Record couldn\'n inserted. Please try again.');
}
else{
$this->session->set_flashdata('success', 'News created.');
redirect('admin/news');
}
}
}
$this->load->view('admin/template/header');
$this->load->view('admin/news/create',$this->data);
$this->load->view('admin/template/footer');
}
if some one have idea to submit form for both the case, then help me to short-out this problem. thanks.....
try this code just put one if condition on file name is not empty
function create(){
$this->data['categories'] = $this->News_model->getNewsCategories();
$this->data['status'] = $this->status;
if($this->input->post()){
$title =$this->input->post('title');
$slug =$this->input->post('slug');
$desc =$this->input->post('desc');
$category_id =$this->input->post('category');
$file_name = $_FILES['image']['name'];
if(!empty($file_name)){
$upload_path = FCPATH.'assets/uploads/news';
$field_name ='image';
$temp_file_name = $this->file[$field_name]['name'];
$file_name = time().'_'.$this->randomString(10).'.'.$this->getFileExtension($temp_file_name);
if(!$this->uploadImage($upload_path,$file_name,$field_name)){
$this->session->set_flashdata('error', $this->file_error);
}
else{
$this->createThumbnail($this->file_data['full_path'], $upload_path.'/thumb/'.$file_name,75,50);
}
}
if(!$this->News_model->insertNews($title,$slug,$desc,$category_id,$file_name)){
$this->session->set_flashdata('error', 'Record couldn\'n inserted. Please try again.');
}
else{
$this->session->set_flashdata('success', 'News created.');
redirect('admin/news');
}
}
$this->load->view('admin/template/header');
$this->load->view('admin/news/create',$this->data);
$this->load->view('admin/template/footer');
}
You can check file have uploaded or not by this:
if (isset($_FILES['upload_field_name']) && is_uploaded_file($_FILES['upload_field_name']['tmp_name'])) {
//load upload class with the config, etc...
$this->load->library('upload', $config);
}

How to make codeigniter sticky form when error comes from upload file field

I have a form that allows user to upload multiple files. Additionaly user have to fill other input text fields like address, email etc.
I don't know how to make form sticky if error comes from file uploading and how to access error messages set by form_validation library. Because if for example one of the filetype is not allowed I use redirect to step out of the loop responsible for uploading every file.
To keep upload error message I use $this->session->set_flashdata('errors', $errors) but then I'm not able to use error messages from form_validation library ( echo form_error('field_name) ) and display user input in form fields ( echo set_value('field_name'))
Here is controller method responsible for file upload and data insert:
function do_upload()
{
// load form validation library
$this->load->library('form_validation');
// set validation rules
$this->form_validation->set_rules('email','Email','trim|required|valid_email|matches[confirm_email]');
$this->form_validation->set_rules('confirm_email','Email','trim|required|valid_email');
$this->form_validation->set_rules('delivery_name','Nazwa','trim|required');
$this->form_validation->set_rules('delivery_street','Ulica','trim|required');
$this->form_validation->set_rules('delivery_city','Miasto','trim|required');
$this->form_validation->set_rules('delivery_postcode','Kod pocztowy','trim|required');
$this->form_validation->set_rules('delivery_country','Kraj','trim|required');
$this->form_validation->set_rules('captcha','Captcha','callback_check_captcha');
if ( $this->form_validation->run() )
{
// if validation passes I insert form data into database and I start to upload
// all the files using foreach loop
$config['upload_path'] = './uploads/upload_directory';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$upload_data = array();
$task_address_id_array = $this->MTasks->insertNewTask($task_directory);
$task_id = $task_address_id_array['task_id'];
$address_id = $task_address_id_array['address_id'];
// iterate over files to be uploaded
foreach ($_FILES as $key => $value)
{
if (!empty($value['name']))
{
$this->upload->initialize($config);
$result = $this->upload->do_upload($key);
if (!$result)
{
// if upload error occurs I need to redirect to break the loop
$errors = $this->upload->display_errors();
$this->session->set_flashdata('errors', $errors);
redirect();
}
else
{
$current_upload_data = $this->upload->data();
// insert upload data to database
$this->MTasks->insertFileInfo($current_upload_data,$task_id);
}
}
}
redirect('upload/success_message');
}
else
{
$this->load->view('include/header');
$this->load->view('upload_form',$this->data);
$this->load->view('include/footer');
}
}
The main problem is you can not put as validation rule file uploads, because the library executes one by one.
A simple solution is to validate your fields and then validate the file upload. Example:
<?php
function save()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->form_validation->set_error_delimiters('', '<br />');
echo validation_errors();
exit();
}
else
{
if($_FILES['imagen']['error'] != 4)
{
$image $this->_manage_image($this->input->post('name'));
}
else
{
exit('No empty field!');
}
# save in your bd o success message...
}
}
function _manage_image($name)
{
# config
$config['upload_path'] = './images/products/';
$config['allowed_types'] = 'gif|jpg|png';
// ...
$config['file_name'] = $name;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image'))
{
# errors...
echo $this->upload->display_errors();
exit();
}
else
{
# upload
return $img_data['file_name'];
}
}

Codeigniter - attaching 2 files to email that have been uploaded from user form to webserver

I have a form which when filled out uploads 2 files (one is a doc,docx, or pdf and the other is an image file). My controller validates the form ok and uploads the files to the server. My problem seems to be accessing the array of the files uploaded!?
I've referenced the array in a variable called $uploaded in the form and from the error it appears the actual array seems to be called 'success' (I pressume it gets this name from the upload.php library) I think this then contains 2 elements called [0] and [1] which each contain another array of the file attributes.
I'm going round in circles basically trying to attach these two files to an email. I hope someone will be able to help a novice out? I will place the controller code below, if you need any other code please let me know.
EDIT: I have updated the code below
I've now had a bit of success (not much): The for loop is now attaching 1 of the files, but twice. So either my code is not indexing the array correctly or the array is not storing both file information?
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
$this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
$this->form_validation->set_rules('dob', 'Date of Birth', 'required');
$this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
$this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
$this->form_validation->set_rules('address_city', 'City', 'required|alpha');
$this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
$this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
$this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
$this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
$this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
$this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
$this->form_validation->set_rules('end_date', 'Course End Date', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('home');
}
else
{
//Display Success page
$this->load->view('formsuccess');
//Send Email
$this->load->library('email');
//Array helper
$this->load->helper('array');
//Upload files to server
$this->load->library('upload');
$config['upload_path'] = './attachments'; //if the files does not exist it'll be created
$config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
$config['max_size'] = '4000'; //size in kilobytes
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files
$data = $this->upload->data();
for ($i = 0; $i <= 1; $i++) {
// ignore file that have not been uploaded
//if (empty($_FILES['uploaded'.$i])) continue;
//get the data of the file
//$fileName = $data['uploaded'.$i]['name'];
$filepath = $data['full_path'];
//add only if the file is an upload
echo $data['full_path'];
$this->email->attach($filepath); //$mail->AddAttachment($filePath, $fileName);
}
$this->email->from('your#example.com', 'Your Name');
$this->email->to('t.bryson#shu.ac.uk');
$this->email->subject('Application for Accommodation (Non-SHU)');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();
}
}
function alpha_dash_space($str_in)
{
if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
$this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
return FALSE;
} else {
return TRUE;
}
}
}
?>
Worked it outeventually, with some help from google:
$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files
var_dump($uploaded);
$data = array('uploaded_data');
//Attach the 2 files to email
foreach($_FILES as $key => $value){
//var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
$file = $uploaded['success'][$key]['full_path'];
$this->email->attach($file);
//unlink($file);
}

struggling with a ajax upload and PHP

I need some help please,
I am upload loading an image with the help of a jquery uploader so that the user can get a preview of the image and I can jCrop on it, my problem is that I then need to save the file name, but I cannot get to the variable I create in the upload function,
The process that users goes throug is as follows,
User fills in their details
User selects and image to up load and hits the upload button
Upload function is run, and the image is returned to the view
User selects an area of the image that will be cropped and saved
User carries on filling form out
Save the items including the filename
Below is my PHP code, which I believe is where the variable goes missing and cannot be used in the add function,
Add() - The function that the form submits too
public function add()
{
$this->form_validation->set_rules('title','title', 'required|trim|min_length[2]');
$this->form_validation->set_rules('firstname', 'firstname', 'required|trim|alpha');
$this->form_validation->set_rules('surname', 'surname', 'required|trim|alpha');
$this->form_validation->set_rules('email', 'email address', 'required|trim|valid_email');
$this->form_validation->set_rules('postcode', 'postcode', 'required|trim|min_length[6]|max_length[9]');
$this->form_validation->set_rules('company_name', 'company_name', 'required|trim');
$this->form_validation->set_rules('company_summary', 'company_summary', 'required|trim|max_length[3000]');
$this->form_validation->set_rules('alternative_ads', 'alternative ads', 'required|trim|prep_url');
$this->form_validation->set_rules('facebook_url', 'Facebook URL', 'required|trim|prep_url');
$this->form_validation->set_rules('twitter_url', 'Twitter URL', 'required|trim|prep_url');
if($this->form_validation->run() == FALSE)
{
$this->template->build('admin/users/add');
}
else
{
//group the post data together soe that we can save the data easily.
$user = array(
'firstname' => $this->input->post('firstname'),
'surname' => $this->input->post('surname'),
'email' => $this->input->post('email'),
'postcode' => $this->input->post('postcode'),
'date_registered' => date("d-m-y h:i:s", time())
);
if(!$this->users_model->insert($this->input->xss_clean($user)))
{
$data['error'] = "We could not save you details, please try again";
$this->template->build('/users/admin/add', $data);
}
$employer = array(
'company_name' => $this->input->post('company_name'),
'company_summary' => $this->input->post('company_summary'),
'logo' => $this->file['file_name'],
'alternative_ads' => $this->input->post('alternative_ads'),
'facebook_url' => $this->input->post('facebook_url'),
'twitter_url' => $this->input->post('twitter_url'),
'user_id' => $this->db->insert_id()
);
if(!$this->employer_model->insert($this->input->xss_clean($employer)))
{
$data['error'] = "We could not save you details, please try again";
$this->template->build('/users/admin/add', $data);
}
else
{
die(print_r($this->file));
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = '/media/uploads/users/' . $this->file['file_name'];
$config['thumb_marker'] = TRUE;
$config['x_axis'] = $this->input->post('x');
$config['y_axis'] = $this->input->post('y');
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
$data['error'] = "We could not crop you image, please try again. If the problem persists please contact us";
$this->template->build('/admin/users/add', $data);
}
$this->session->set_flashdata('success', 'You have successfully added an employer');
redirect('/admin/users/manage');
}
}
}
And the upload function that the jquery uploader calls
private function upload()
{
$config['upload_path'] = "./media/uploads/users";
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
die(print_r($error));
}
else
{
$this->file = $this->upload->data();
$msg = $this->file;
echo json_encode($msg);
}
}
To recap I am setting $this->file in upload() and trying to use it in the add() function.
You'll have to return something from the upload() function in order to use it in the add() function. Depending on your app and it's set up this may or may not break your ajax.
An alternative would be to set session, for the bits you need.
$this->file will only be in the scope of the upload. So like Ross mentioned have the upload echo the image in the json and then do something with it in the form. Like add it to a hidden input that is then sent with the post data.

Categories