Why am I getting "Call to undefined function get_filenames()"? - php

I'm trying to get a list of file names using the url helper but I'm getting the following error:
Call to undefined function get_filenames()
I auto-loaded the url helper and then explicitly loaded it in the Controller and I still get the error. Why am I getting this error?
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Downloads extends CI_Controller {
public function index()
{
$this->load->helper("url");
$this->template->write_view('content', 'download_view');
$this->template->render();
}
}
View:
<? get_filenames("/somefolder"); ?>

You have loaded the url helper which does not contain anything similar to get_filenames().
Maybe you meant to load the file helper instead:
$this->load->helper('file');

You don't have a function named get_filenames() defined. May be need to include the file correctly. Trying using $this with the name.

Related

codeigniter cli is not working showing errors with ./system/core/Log.php on line 131

I am trying to use codeigniter function via cli but it is showing me error
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Msg_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function send_in_background($key_with_info){
echo "working";
log_message("error","Key ".$key_with_info);
}
}
CALLING IT LIKE -:
php index.php cli/msg_controller/send_in_background '12547'
ERROR
PHP Warning: mkdir(): Invalid path in /var/www/html/pro/system/core/Log.php on line 131
If i am calling same function from browser it is working fine.
http://localhost/pro/cli/msg_controller/send_in_background/12547
The correct way to run via CLI is
php path/to/index.php controller method params
In your case:
php index.php msg_controller send_in_background 12547
Give that a try

How to call a function in helper from outside codeigniter (ci) - tried the solution from other stackoverflow links but doesn't work

I have created a function in application/helpers/ which works fine when accessing from ci.
Now I want to call the same function from outside ci i.e., from core PHP file. But I'm not able to do so.
Below is the approach tried.
Created a test.php outside ci and below is the code:
<?php
$filepath = dirname(__FILE__);
ob_start();
require_once($filepath.'/ci/index.php');
ob_get_clean();
return $CI;
?>
In another core PHP file, test2.php, below is the code:
$CI = require_once('test.php');
echo $CI->config->item('base_url');
some_helper_function($param1, $param2);
Error message:
Fatal error: Call to a member function item() on a non-object in <path>/Utf8.php on line 47
Folder structure:
test.php
test2.php
ci/application/helpers/test_helper.php (contains some_helper_function())
Any suggestions?
Create a file and put the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('test_function'))
{
function test_function()
{
//your functionality//
}
}
Save this to application/helpers/ . save it as "test_helper.php"
The first line exists to make sure the File cant be included and run from outside the Code Igniter.
Then in your controller or Model
$this->load->helper('test_helper');
You can use any function from that helper page.
Else
your-project-name\application\config\autoload.php
$autoload['helper'] = array('test_helper');
So that you can use that helper function in any controller or model. Not to initialize in each and every controller or model like previously said.

How can I change the welcome.php in Codeigniter to something like index.php?

I'm currently learning Codeigniter. As you know, there is a default file called welcome.php in the controller when you first install the package.
I tried to modify that page to index.php, and here is the code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Index extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
I also changed the route.php in the config file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Then I access the page by entering this path: http://localhost/CI/index.php, but it says there are two errors:
Message: Undefined property: Index::$load. Filename: controllers/Index.php
Message: Call to a member function view() on null. Filename:controllers/Index.php
Did I forget to change something else in order to make it work?
I downloaded CI3.0.2 and tried your code in my computer. I encountered the same problem, and with a few time debug I found what caused this problem.
Your class is Index and your function is index two, in php class when you don't define constructor __construct it will try to find if there's a method that have same name with class nameIndex, so in this situation index function is the constructor of class Index. if this confuse you see this document : constructor php official document
Solution:
class Index extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('welcome_message');
}
}

CodeIgniter does not autoload my core extension

As I am very pleased of the command show_404() which you can call everywhere to show a 404-Error-Page, I did want to implement a show_403() for requests without permissions.
I created the file application/core/MY_Exceptions.php and added the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
}
Then I'll call it in a controller application/controllers/Welcome.php like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
// show_404(); // <-- this works!
show_403(); // <-- this works not!
}
}
and I keep getting the following error in the browser, when I access the controllers index method:
Fatal error: Call to undefined function show_403()
As you may have noticed I even tested this on a vanilla installation of CodeIgniter, so you should be able to reproduce this error with just these two files.
I know that I could load the extension manually, but that has not the elegance of using show_403() wherever and whenever I want...
Routing is set up correctly, CodeIgniter is version 3.0.3, PHP is version 5.6.12., filesystem permissions to application/core/MY_Exceptions.php have even been set to 777 for debugging purposes.
Your exception class is not loaded, so its giving that error.
Load your class like this
$excep = load_class('Exceptions', 'core', $this->config->item('subclass_prefix'));
echo $excep->show_403();// will echo test
One more suggesion is to use helpers for this,
Add your code in application/helpers with file name error_helper
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
Calling your function
$this->load->helper('error_helper');
echo show_403();

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

Categories