iam trying to file writing using following code is not done
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('file');
}
public function index()
{
$data = "Some file data";
if (!write_file(base_url()."test.txt", $data))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
}
After i changed the code its done
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('file');
}
public function index()
{
$data = "Some file data";
if (!write_file('M:\xampp\htdocs\tt\text.txt', $data))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
}
Any one can explain what is the problem in above code
When creating a file with write_file make sure your folder/directory is there where you would like to create file
Then I would recommend using FCPATH
$data = "Some file data";
if (write_file(FCPATH . '/document/text.txt', $data) == FALSE)
{
echo 'Unable to write the file';
} else {
echo 'File written!';
}
My directory
application
document
system
index.php
Related
I have implemented Maatwebsite/Laravel-Excel with input file, this works fine.
public function import(Request $request)
{
$path = $request->file('file');
if (!is_file($path))
{
dd("file excel missing");
}
Excel::import(new ProductsSerialsImport,request()->file('file'));
return redirect()->back()->with('success', 'File updated!');
}
Route::post('products_serials/import', 'ProductsSerialsController#import')->name('products_serials.import');
I done something of this, but don't understand where I can receive excel file without pass through input file.
public function import(Request $request)
{
$path = "storage/excel/file.xlsx";
if(!File::exists($path)) {
dd("missing excel file!");
}
Excel::import(new ProductsSerialsImport, $path);
return redirect()->back()->with('success', 'File updated!');
}
How I can update excel file from route => 'products_serials/import' skipping upload file through the input file?
Thanks in advance!
There's a constructor. from docs it will be like this
ProductsSerialImport.php
class ProductsSerialImport implements ToCollection {
private $file;
public function __construct($file)
{
$this->file = $file;
}
public function collection(Collection $row)
{
foreach($row as $r) {
Model::create(['name', $this->file]); // Just for example
}
}
}
YourController.php
Excel::import(new ProductsSerialsImport(request()->file('file')));
I am creating a login system and for some reason it redirects back to the login page even after i have put in the correct email and password. The called model function doesnt seem to work. Here is my code:
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if ($this->form_validation->run()){
redirect('login/valid_credentials');
}else{
$this->load->view('login_form');
//return false;
}
}
function valid_credentials(){
$this->load->model('login_model');
// oh i see
if ($this->login_model->match_login()){
//return true;
$data = array(
'id'=>$q['id'],
'email'=>$this->input->post('email'),
'is_logged_in'=> 1);
$this->session->set_userdata($data);
$this->load->view('dashboard_view', $data);
}else{
$this->load->view('login_form');
}
}
Model:
class Login_model extends CI_Model{
public function match_login(){
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$q = $this->db->get('user');
if($q->num_rows()== 1){
return true;
}
}
}
View:
<?php
echo "<h1 class ='col-lg-10 col-lg-offset-5'>SIGN IN</h1>";
echo form_open('login/validation', $grid);
echo "<h2>Client</h2>";
"<h3>Please Login</h3>";
echo validation_errors();
echo "<p> Email: </br>";
echo form_input ('email');
echo "</p>";
echo "<p> Password: </br>";
echo form_password ('password');
echo "</p>";
echo "<p>";
echo form_submit ('submit', 'Signin');
echo "</p>";
echo form_close();
?>
For some reason, it is not working, Am not sure what I am missing.
No Need to validate form in one function and check login in another function. use one function for do all.
Changes
Controller code change and optimized
Model code changed
Some References
CodeIgniter Form Validation in FromGet.com (Personally Recommend this site )
Form Validation in Codeigniter.com
Try this
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if ($this->form_validation->run() == FALSE )
{
$this->load->view('login_form');
//return false;
}
else{
$this->load->model('login_model');
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->login_model->match_login($email, $password);
if ($result ==false) {
echo "Invalid Cardinals";
}
else
{
$session = array(
'id'=>$result[0]['id'],
'email'=>$this->input->post('email'),
'is_logged_in'=> 1
);
if (!$this->session->set_userdata($session)) {
$data['modelData'] = $result;
$data['sessionData'] = $session;
$this->load->view('dashboard_view', $data);
}
else {
echo "Error in session";.
}
}
}
}
Model
class Login_model extends CI_Model{
public function match_login($email, $password){
$this->db->where('email', $email);
$this->db->where('password', md5($password));
$query = $this->db->get('user');
$result = $query->result_array();
if (empty($result))
{
return false;
}
else
{
return $result;
}
}
}
When I wrote the following code in controller folder, naming the file LoginController.php,
<?php
class LoginController extends CI_Controller
{
public function index()
{
$this->load->view('login');
}
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else{}
}
?>
the following parsing warning was signalled:
-Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in C:\xampp\htdocs\CodeIgniter\application\controllers\LoginController.php on line 14
You have written if clause outside the check function it should be like this
<?php
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else{}
}
?>
Your If Else doesnt include in any function... put it in function braces above this way
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == FALSE){
$this->load->view('login');
}
else{
}
}
you have defined if-else out side of the function, add it inside the function
public function check()
{
$this->form_validation->set_rules('username','Username','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
}
}
I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result
I've heard about codeigniter couple of times so i was curious and thought why not. I took some tutorials and was very delighted to see how the framework worked.
Now i've the following problem i want to passing the data i've made in my model trough my controller and showing this in my view but i always run to the folowing error: Fatal error: Call to a member function query() on a non-object in C:\wamp\www\codeigniterTest\application\models\leden_model.php on line 9. The funny thing about this error is, when i google on this issue a lot of forum topics is about this issue but nowhere i get the right answer. my code looks like this.
codegniter version 2.03
class Leden extends CI_Controller {
function __construct(){
parent::__construct();
}
function index()
{
$this->load->model('leden_model');
$ledenModel = new Leden_model();
$data = $ledenModel->allLeden();
$this->load->view('leden_overzicht',$data);
}
}
<?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden(){
$query = $this->db->query("SELECT * FROM leden");
foreach ($query->result_array() as $row)
{
echo $row['Naam'];
echo $row['Achternaam'];
echo $row['Email'];
}
return $query;
}
}
?>
When i'm doing the query in my controller then i'm getting the results i want, why not in my model?
my question is what am i doing wrong?
Leden_model.php
?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden()
{
$data = array();
$this->db->select();
$query = $this->db->get('leden');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Leden_controller.php
?php
class Leden extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('leden_model');
}
function index() {
$data['leden_data'] = $this->ledenModel->allLeden();
$this->load->view('leden_overzicht',$data); }
}
leden_overzicht.php
?php
if (count($leden_data))
{
foreach ($leden_data as $key => $list)
{
echo $list['Naam'] . " " . $list['Achternaam'] . " " . $list['Email'] . "";
} }
else {
echo "No data."; }
did you load database? example:
$this->load->database();