Codeignter 3 Image upload Code not working - forms

I am trying to upload image using the code igniter. But it is giving me the following error
You did not select a file to upload
I have tried, many different sources, but still getting the error.
The PHP Code is below:
public function save_post()
{
$image_path = './uploads/';
$config['upload_path'] = $image_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_size'] = 5000;
// $config['max_width'] = 1024;
// $config['max_height'] = 1024;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('postimage')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
$this->add_post($error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->add_post($data);
}
die;
$data = array(
'page_title' => $this->input->post('ptitle'),
'page_slug' => $this->input->post('pslug'),
'page_body' => $this->input->post('pbody'),
'page_keyword' => $this->input->post('pkeywords'),
'page_description' => $this->input->post('pdesc'),
'page_image' => $this->input->post('postimage'),
);
$this->admin_model->save_post($data);
redirect(base_url() . 'admin/all_posts');
}
My View is below
<form method="post" action="<?php echo base_url(); ?>admin/save_post" enctype ="multipart/form-data ">
<div class="form-group">
<label for="postimage">Feature Image</label>
<input type="file" class="form-control" id="postimage" name="postimage" required>
</div>
<div> <?php if(isset($error)) echo $error; ?></div>
<div class="form-group">
<input type="submit" value="publish" name="submit" id="post-submit" class="btn btn-success waves-effect waves-light">
</div>
</form>

In views replace this code
<form method="post" action="<?php echo base_url(); ?>admin/save_post" enctype ="multipart/form-data ">
from this code
<?php echo form_open_multipart('admin/save_post');?>

The PHP Code is below:
enctype ="multipart/form-data " -> value inside attribute with extra spaces
replace this code
enctype ="multipart/form-data"

Related

Passing Data from a Controller to a View (not displaying it) then passing back to different Controller in Codeigniter

I am trying to a create a View which provides a summary table of various site members profiles - then each summary has a button, that once clicked, takes the user to that member's full profile. I can get the summary page to work properly, but I cant get the second part -where the button takes the user to the members full profile to work.
Here is my controller:
public function network_barre()
{
$this->load->model("Profiles_model");
$style ='barre';
$profilesubdata["fetch_data"] = $this->Profiles_model->fetch_data($style);
$this->load->view('templates/header');
$this->load->view('pages/page-network_barre', $profilesubdata);
$this->load->view('templates/footer');
Here is my Model "Profiles Model"
function fetch_data($style)
{
$this->db->select("username, email, style, about");
$this->db->from("profiles");
$this->db->where('style', $style);
$query = $this->db->get();
return $query;
}
Here is the view
<div class="container">
<div class="row d-flex justify-content-center card-top">
<?php
if($fetch_data->num_rows()>0)
{
foreach ($fetch_data->result() as $row)
{
?>
<div class="col-lg-4 col-md-6">
<div class="card card-warning wow zoomInUp mb-4 animation-delay-5">
<div class="withripple zoom-img">
<img src="<?=base_url();?>assets/img/demo/avatar4.jpg" class="img-fluid">
</div>
<div class="card-body">
<span class="badge badge-warning pull-right"><?php echo $row->style; ?></span>
<h3 class="color-warning"><?php echo $row->username; ?></h3>
<p><?php $string=character_limiter($row->about, 255, '&#8230(More in Profile)') ; echo $string?></p>
<div class="row">
<div class="col text-center">
<!-- BUTTON -->
<form method="POST" action="<?php echo base_url(); ?>public_area/gotopublicprofile/<?php echo $row->email; ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<button type="submit" class="btn btn-raised btn-sm btn-warning" name="submit" value="submit"><i class="zmdi zmdi-account"></i>Profile</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php //this tag close out php tags above
}
}
?>
</div>
</div>
here is Controller gotopublicprofile
function gotopublicprofile()
{
$this->load->model("profiles_model");
$email = '$this->uri->segment(3)';
$profiledata["fetch_profiledata"] = $this->Profiles_model->fetch_profiledata($email);
$this->load->view('templates/header');
$this->load->view('pages/page-profilepublic', $profiledata);
$this->load->view('templates/footer');
}
And here is the fetch_profiledata function in the Model:
function fetch_profiledata($email)
{
$this->db->where('email', $email);
$query = $this->db->get("profiles");
return $query;
}
I always get these error messages:
Undefined property: Public_area::$Profiles_model
AND
Call to a member function fetch_profiledata() on null
I am sure I am just missing something simple, as lots of websites use this approach.
Thanks in advance!
So I figured it out..it was a combination of little things:
First
$email = '$this->uri->segment(3)';
became
$email = $this->uri->segment(3);
Thanks #kirb! and
function gotopublicprofile()
{
$this->load->model("profiles_model");
became
function gotopublicprofile()
{
$this->load->model("Profiles_model");
Thanks #Atal Prateek!

codeigniter upload form not working

I've never used code igniter and I'm trying to make a quick admin form that includes an image upload input. My admin form is up and has a route/url that I can reach, but the save function is not working correctly. I'm getting a 404 error when I click my submit button.
I believe the issue is with the line form_open_multipart('dashboard_save/do_upload') in views/admin/Dashboard.php. I don't think the do_upload function is being reached. Any ideas where I'm going wrong?
*new detail: I am able to reach my controller with form_open_multipart('dashboard_save') using an index function... but I'm not able to reach any other function such as form_open_multipart('dashboard_save/upload') using an upload function in controller dashboard_save.
CONTROLLERS
controllers/admin/Dashboard_save.php
<?php
class Dashboard_save extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
// die('got here 2');
$this->load->view('admin/dashboard_view', array('error' => ' ' ));
}
public function do_upload()
{
// die('got here!!');
$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('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('dashboard_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('dashboard_save', $data);
}
}
}
?>
VIEWS
views/admin/Dashboard.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
<div id="main-wrapper">
<div id="main-container">
<div id="main-body">
<div id='main-form-body'>
<p>Configure front end here.</p>
<div id='admin-form-container'>
<?php echo form_open_multipart('dashboard_save/do_upload');?>
<form id="admin-form" method="" action="">
<div class='form-field'>
<div class='label-wrapper'><label for='main_img'>Main Image</label></div>
<input type = "file" name = "userfile" size = "20" />
</div>
<div class='form-field'>
<button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
views/admin/Dashboard_save.php
<html>
<head><title>Dashboard Save</title></head>
<body>
<h3>testing dashbord submit</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Hi first to check your do_upload function has been called or not . if yes then to please check you have loaded in upload library or not . if not so please upload library and than to use below code
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
<div id="main-wrapper">
<div id="main-container">
<div id="main-body">
<div id='main-form-body'>
<p>Configure front end here.</p>
<div id='admin-form-container'>
<form id="admin-form" method="post" action="<?php echo base_url()?>/dashboard_save/do_upload" enctype="multipart/form-data">
<div class='form-field'>
<div class='label-wrapper'><label for='main_img'>Main Image</label></div>
<input type = "file" name = "userfile" size = "20" />
</div>
<div class='form-field'>
<button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
You have used two times the form tag. This is why its taking last one and showing error. Remove one FORM tag. Here is your view file code revised
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
<div id="main-wrapper">
<div id="main-container">
<div id="main-body">
<div id='main-form-body'>
<p>Configure front end here.</p>
<div id='admin-form-container'>
<?php $attributes = array('id' => 'admin-form'); echo form_open_multipart('dashboard_save/do_upload', $attributes);?>
<div class='form-field'>
<div class='label-wrapper'><label for='main_img'>Main Image</label></div>
<input type = "file" name = "userfile" size = "20" />
</div>
<div class='form-field'>
<button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
</div>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</body>
</html>
No test it. I have removed form open & close tag which you wrote manually, at same time use only form_open_multipart() & form_close() for accomplish same task

how to get the name of image in input , code igniter edit form

I am trying to edit a picture that I've already uploaded, so when Click on edit i cant not get the name of the image in the input. here is my code in controller:
It means I want to edit the details of an item but i don't want to edit the image of it.
controller
//method to edit category
public function edit_menu($id) {
if($this->isLogin()) {
//object/instance of menu
$menu_list = new Menu();
//calling method of , menu model
$menu_list->load($id);
//print_r($menu_list);
$menu_list_array = (array)$menu_list;
//object/instance of category
$category = new Category();
//calling method of category model
$category_list = $this->Category->get();
//creating an array to hold data
$category_form_options = array();
//iterate through loop
foreach ($category_list as $id => $category) {
$category_form_options[$id] = $category->category_name;
}
//creating an array to hold data for category and menu, to pass data to view
$data = array();
//category key of data array to hold category details
$data['category'] = $category_form_options[$menu_list_array['category_id']];;
//menu key of data array to hold menu details
$data['menu'] = $menu_list;
//CI built-in method to have three params, first is field id, second is human readable lable to show in case of error, rules include required, type.
$this->form_validation->set_rules(array(
array('field' => 'category_id','label' => 'Category', 'rules' => 'required',),
array('field' => 'type','label' => 'Type','rules' => 'required',),
array('field' => 'menu_name', 'label' => 'Menu Name', 'rules' => 'required',),
array('field' => 'description','label' => 'Description','rules' => 'required',),
));
if (empty($_FILES['avatar']['name'])){
$this->form_validation->set_rules('avatar', 'Avatar', 'required');
}
//CI method to error delimiters,
//https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#errordelimiters
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
if (!$this->form_validation->run()) {
$datas['title'] = "";
$this->load->view('header', $datas);
//this will load data
$this->load->view('category_form_edit', array('category_form_options' => $category_form_options, 'data' =>$data, ));
$this->load->view('footer');
}
else {
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->do_upload('avatar');
$data_upload_files = $this->upload->data();
//record from global post variable to model fields.
$menu_data = array();
$menu_data['category_id'] = $menu_list->category_id = $this->input->post('category_id');
$menu_data['type'] = $menu_list->type = $this->input->post('type');
$menu_data['menu_name'] = $menu_list->menu_name = $this->input->post('menu_name');
$menu_data['description'] = $menu_list->description = $this->input->post('description');
$menu_data['per_gram'] = $menu_list->per_gram = $this->input->post('per_gram');
$menu_data['per_quantity'] = $menu_list->per_quantity = $this->input->post('per_quantity');
$menu_data['per_dozen'] = $menu_list->per_dozen = $this->input->post('per_dozen');
$menu_data['avatar'] = $menu_list->avatar = $data_upload_files['file_name'];
$menu_data['updated_by'] = $menu_list->updated_by = $_SESSION['user_id'];
$menu_data['updated_on'] = $menu_list->updated_on = date('Y-m-j H:i:s');
//update model
$id = $menu_list_array['menu_id'];
$where = array('menu_id' => $id);
$menu_list->update('menus', $menu_data, $where);
$this->load_menu();
}
}
}
view
<?php echo form_open_multipart('', 'fscontrol/edit_menu');?>
<!-- <form method="post"> -->
<div class="form-group">
<label for="category_id">Category Name</label>
<select name="category_id" class="form-control" >
<?php
foreach ($category_form_options as $category_id => $category_name) {
$selected = $data_array['category_id'] == $category_id ? 'selected' : '';
echo '<option ' . $selected. ' value="' . html_escape($category_id) . '">' . html_escape($category_name) . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="type">Type</label>
<input type="text" class="form-control" name="type" value="<?php echo $data_array['type']; ?>">
</div>
<div class="form-group">
<label for="menu_name">Menu Name</label>
<input type="text" class="form-control" name="menu_name" value="<?php echo $data_array['menu_name']; ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="<?php echo $data_array['description']; ?>">
</div>
<div class="form-group">
<label for="per_gram">Per gram</label>
<input type="text" class="form-control" name="per_gram" value="<?php echo $data_array['per_gram']; ?>">
</div>
<div class="form-group">
<label for="per_quantity">Per quantity</label>
<input type="text" class="form-control" name="per_quantity" value="<?php echo $data_array['per_quantity']; ?>">
</div>
<div class="form-group">
<label for="per_dozen">Per dozen</label>
<input type="text" class="form-control" name="per_dozen" value="<?php echo $data_array['per_dozen']; ?>">
</div>
<div class="form-group">
<label for="avatar">Photo</label>
<input type="file" class="form-control" name="avatar" size="20" value="<?php echo $data_array['avatar']; ?>">
</div>
<div class="form-group">
<input type="hidden" name="menu_id" value="<?php echo $data_array['menu_id']; ?>">
<input type="submit" class="btn btn-default" value="Edit"/>
</div>
</form>
If you are using bootstrap template you will not get the image name. You'll see only:
no file chosen
So it is better to use:
<img src="">
, and display image as a thump nail.

Form not POST ing

this is my view where there is a form containig a field and a submit button.
<form id="typeheadForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-xs-3 control-label">Movie</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="state" id="movie_name" />
</div>
</div>
<input type='submit' name='submit' value='Search' class="btn btn-squared btn-default">
</form>
and below is my controller code
public function actionMovies_all()
{
$this->layout = "main";
if ( isset( $_POST[ 'movie_name' ] ) )
{
print_r("success");die();
}
if ( Yii::$app->request->post() )
{
print_r(Yii::$app->request->post());die();
}
}
i am not able to POST the form. what am i doing wrong?
i am getting an error " Bad Request (#400)
Unable to verify your data submission."
Replace <form id="typeheadForm" method="post" class="form-horizontal"> with
<?= \yii\helpers\Html::beginForm('', 'post', ['id' => 'typeheadForm', 'class' => 'form-horizontal']);?>
You are getting bad request because when you create your form manually, you did not include csrf token into it. When you create form with Html::beginForm method it takes care about it internaly.
try this:
public function actionMovies_all()
{
$this->layout = 'main';
if ( isset( $_POST[ 'submit' ] ) )
{
print_r('success');die();
}
if ( Yii::$app->request->post() )
{
print_r(Yii::$app->request->post());die();
}
}

Input Form Using Codeigniter and Bootstrap 3

I'm using Codeigniter, styled with Bootstrap 3 to build a website.
I can't stylize the text-fields built by the PHP/Form Helper, as I'm not sure where to use the tags, every solution I've tried has resulted in either an extra text field, or just the addon appearing, or nothing at all.
Controller
public function __construct ()
{
parent::__construct();
}
public function index ()
{
// Fetch all users
$this->data['users'] = $this->user_m->get();
// Load view
$this->data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function edit ($id = NULL)
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->user_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->user_m->get_new();
}
// Set up the form
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->user_m->array_from_post(array('name', 'email', 'password'));
$data['password'] = $this->user_m->hash($data['password']);
$this->user_m->save($data, $id);
redirect('admin/user');
}
// Load the view
$this->data['subview'] = 'admin/user/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function delete ($id)
{
$this->user_m->delete($id);
redirect('admin/user');
}
public function login ()
{
// Redirect a user if he's already logged in
$dashboard = 'admin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);
// Set form
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);
// Process form
if ($this->form_validation->run() == TRUE) {
// We can login and redirect
if ($this->user_m->login() == TRUE) {
redirect($dashboard);
}
else {
$this->session->set_flashdata('error', 'That email/password combination does not exist');
redirect('admin/user/login', 'refresh');
}
}
// Load view
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
public function logout ()
{
$this->user_m->logout();
redirect('admin/user/login');
}
public function _unique_email ($str)
{
// Do NOT validate if email already exists
// UNLESS it's the email for the current user
$id = $this->uri->segment(4);
$this->db->where('email', $this->input->post('email'));
!$id || $this->db->where('id !=', $id);
$user = $this->user_m->get();
if (count($user)) {
$this->form_validation->set_message('_unique_email', '%s should be unique');
return FALSE;
}
return TRUE;
}
}
View
<div class="modal-body">
<?php echo validation_errors(); ?>
<?php echo form_open();?>
<div class="container">
<div class="modal-header">
<h3>Log in</h3>
<p>Please log in using your credentials</p>
</div>
<table class="table">
<tr>
<td>Email</td>
<td><?php echo form_input('email'); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Log in', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<div class="modal-footer">
© <?php echo date('Y'); ?> <?php echo $meta_title; ?>
</div>
<?php echo form_close();?>
</div>
</div>
</div>
Bootstrap
<form class="form-signin" role="form">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" placeholder="email" required autofocus value="<?php echo set_value('email') ?>">
<input type="password" class="form-control" placeholder="password" required value"<?php echo set_value('password') ?>">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
You just pass everything as an array to form_input
<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email')]); ?>
Here's your entire form as presented,
<?php echo form_open('controller/method', ['class' => 'form-signin', 'role' => 'form']); ?>
<h2 class="form-signin-heading">Please sign in</h2>
<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email'), 'placeholder' => 'Email']); ?>
<?php echo form_password(['name' => 'password', 'id' => 'password', 'class' => 'form-control', 'placeholder' => 'Password']); ?>
<label class="checkbox">
<?php echo form_checkbox(['name' => 'remember_me', 'value' => 1]); ?>
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<?php echo form_close(); ?>