Error while writing file in CodeIgniter - php

I am writing php codeigniter from scratch
and i am trying know write_file function in file helper
this is my controller
<?php
class Files extends CI_Controller {
var $file;
function Files()
{
$this->file = "application" . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR . "hello.txt";
}
function write_file(){
$data = "Hello World !! :)";
write_file($this->file,$data ,'a');
echo "writing is finishing...";
}
}
and my version is 2.1.4, befog I put 'a' it works great but after I add it
it gives me this error
Call to undefined function write_file() in C:\wamp\www\test7\application\controllers\files.php

If you want to use a helper function in codeigniter, you'll need to load it first:
$this->load->helper('file');
Just add this line either in the your controller's constructor function or before you use write_file()

Related

PHP MVC - How to get value of variable in include or require file

I'm learning and writting MVC by php. I set value of variable from file controller and i want to get value in file views but it's not working.
My controller:
class BooksController extends Controller
{
function list()
{
$books = $this->model->get_all();
render('view/book/list');
}
}
My views list.php:
<?php
var_dump($books);
?>
My function render:
function render($file_name,$extent="php"){
$file_name = 'view/'.$file_name.'.'.$extent;
if(!file_exists($file_name))
throw new Exception("Can't open file ".$file_name);
require_once $file_name;
}
Result: NULL
I trying replace:
render('view/book/list');//not work
to
require_once('view/book/list.php');//work
Pls tell me, What is wrong?
You are passing view/book/list.php to
$file_name = 'view/'.$file_name.'.'.$extent;
Which will result in $filename = 'view/view/book/list.php.php'
Dumping your variables on the screen or in a log file might help you in the future in situations like that. See http://php.net/manual/en/function.var-dump.php
Try
render('book/list');
You have 2 times view when it goes in your function render and pass $books in your function render also.
class BooksController extends Controller
{
function list()
{
$books = $this->model->get_all();
render('book/list',$books);
}
}
And in your function render
function render($file_name,$data,$extent="php"){
$file_name = 'view/'.$file_name.'.'.$extent;
$books = $data;
if(!file_exists($file_name))
throw new Exception("Can't open file ".$file_name);
require_once $file_name;
}

Codeigniter Controller cannot use function within helper

I have the controller
<?php
class Onetimescan extends CI_Controller{
public function index() {
#LOAD -->>>> LIB ###########
$this->load->library('session'); ## LOAD LIBS: SESSION
$this->load->helper('simpledom');
#LOAD -->>>> MODEL
$this->load->model('scanmodel');
$this->scanmodel->loadurlbasedonsessid($this->session->userdata('session_id')); // echo out inserted domain in loadurlbasedonsessid() func
$sss= $this->session->userdata('tld'); // echo $sss;
$siteToSearch = file_get_html($sss);
foreach($siteToSearch->find('form') as $element){
echo "<h1 style='color:red; '>".$element->action."</h1> <br />";
}
}
}
?>
I'm getting a fatal error of Call to a member function find() on a non-object on this line:
foreach($siteToSearch->find('form') as $element){
simpledom is a helper I loaded at the top (actually called simpledom_helper.php) which has a file_get_html defined like:
[http://pastebin.com/HaJtKfNb][1]
What am I doing wrong here? I tried definining the function like public function file_get_html{} but that threw errors.
I fixed this all by adding:
$prefix = "http://www.";
//echo "SSS is: ".$sss;
$siteToSearch = file_get_html($prefix.$sss);
FIXED>>
The url was trying to get_file_contents(somedomain.com) and was missing the http://www. as a fully qualified domain. This function get_file_contents seems to require the http://www.
Helper functions should be called like this since they are not objects:
$this->load->helper('simpledom');
$siteToSearch = file_get_html($sss);

PHP spl_autoload_register is doing nothing

i'm updating my code and trying to use spl_autoload_register but IT SIMPLY DOESN'T WORK!!!
I'm using PHP 5.3.8 - Apache 2.22 on Centos / Ubuntu / Win7 and trying to echo something but i get nothing instead... have been trying to make it work for the last 3 hours with no result... this is driving me mad!!!
class ApplicationInit {
// Constructor
public function __construct() {
spl_autoload_register(array($this, 'classesAutoloader'));
echo 'construct working...!';
}
// Autoloading methods
public function classesAutoloader($class) {
include 'library/' . $class . '.php';
echo 'autoload working...!';
}
}
first echo from __construct works but the "classesAutoloader" doesn't work at all, this class is defined in a php file within a folder and i'm calling it from index.php like follows:
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', getcwd() . DS);
define('APP', ROOT . 'application' . DS);
// Initializing application
require(APP.'appInit.php');
$classAuto = new ApplicationInit();
any help is truly appreciated, thanks in advance!
It seems like you're doing the wrong thing. The function that you pass to spl_autoload_register is what's responsible for loading the class file.
Your code is calling
$classAuto = new ApplicationInit();
but by that time, ApplicationInit is already loaded so the autoload function doesn't get called
A more logical way would be for you to to call
spl_autoload_register(function($class){
include 'library/' . $class . '.php';
});
Then when you call
$something = new MyClass();
And the MyClass is not defined, then it will call your function to load that file and define the class.
What is your problem? Your code is working correctly.
class ApplicationInit {
public function __construct() {
spl_autoload_register(array($this, 'classesAutoloader'));
echo 'construct working...!';
}
public function classesAutoloader($class) {
include 'library/' . $class . '.php';
echo 'autoload working...!';
}
}
$classAuto = new ApplicationInit(); //class already loaded so dont run autoload
$newclass = new testClass(); //class not loaded so run autoload

Call to undefined method CI_Controller::Controller()

I have got this controller:
class Start extends CI_Controller{
var $base;
var $css;
function Start()
{
parent::Controller(); //error here.
$this->base = $this->config->item('base_url'); //error here
$this->css = $this->config->item('css');
}
function hello($name)
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testView', $data);
}
}
it tells me in this line:
parent::Controller(); //error here.
Call to undefined method CI_Controller::Controller()
If I remove that line..I get an error for the next line that says..
Call to a member function item() on a non-object
How do I prevent such errors form happening?
If you're using CI 2.x then your class constructor should look like this:
public function __construct()
{
parent::__construct();
// Your own constructor code
}
read more in user guide
In CodeIgniter 2, the constructor is named __constructor and not the class name. So you need to call parent::__construct() instead of parent::Controller()
Here's an article that you can read that shows one major difference between CodeIgniter 1.x and CodeIgniter 2.x
http://ulyssesonline.com/2011/03/01/differences-between-codeigniter-1-7-2-and-2-0-0/
If you're running your Codeigniter project via Xampp or a similar server add the following code to the bottom of your config.php file in the following directory; ci_project/application/config/config.php
function my_load($class) {
if (strpos($class, 'CI_') !== 0) {
if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {
require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');
}
}
}
spl_autoload_register('my_load');
The above code would help to; load class in core folder.
I'm certain this works in the following setup; CI-3+, Xampp, Php5.6, and or 5.6+
Also, you can then decide to create and allow other classes to reference your own Controller (which extends the original CI_Controller) by creating a file named MY_Controller.php in the following directory: ci_project/application/core/ and adding the following code in it;
<?php
class MY_Controller extends CI_Controller {
}
?>
That way you can always just reference or extend the other Classes to your own Controller (MY_Controller) throughout the rest of the project e.g.
class Admin extends MY_Controller {
//your function here
}
I hope this helps.

Create Simple Codeigniter library

For my current project i decided to create a library for some common functionalities.
Ex : Login_check,get_current_user etc.
With my little knowledge i created a simple one but unfortunately its not working.
Here my library :
FileName : Pro.php and located in application/libraries
class Pro{
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->load->database();
}
function show_hello_world()
{
$text = "Hello World";
return $text;
}
}
?>
And i tried to load it on my controller :
<?php
class Admin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('session'));
$this->load->library("Pro");
}
function index()
{
echo($this->Pro->show_hello_world());
}
}
?>
I cant see any erros there...but i am getting a blank page.
Whats wrong with me ??
Thank you .
Edit : I got this error :
Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
One thing I notice: remove the parent::__construct() from your library constructor, because it's not extending anything so has no parent to call.
Also, enable error reporting by setting the environment to "development" in index.php, and you might also want to raise the logging threshold to 4 in config/config.php so you log errors.
Try this simple test-case:
file Pro.php in application/libraries:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
Controller admin.php in application/controllers
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
while your class name is capitalized, all your references to the library when loading it and using it should be lower case. you also do not need the constructor, as the other commenter mentioned.
so instead of:
echo($this->Pro->show_hello_world());
you should have:
echo($this->pro->show_hello_world());
I prefer the standard php autoloader approach, with this you dont need to change your classes at all, you can use your standard classes without modifications
say for instance you class is class 'Custom_Example_Example2' and is stored in libraries
in sub folders you can add this autoloader in the master index.php
make sure it is added below the defined APPPATH constant
//autoload custom classes
function __autoload($className) {
if (strlen(strstr($className, 'Custom_')) > 0 ||
strlen(strstr($className, 'Other1_')) > 0 ||
strlen(strstr($className, 'Other2_')) > 0) {
$exp = explode('_', $className);
$file = APPPATH.'libraries';
if(!empty($exp)) {
foreach($exp as $segment) {
$file .= '/'.strtolower($segment);
}
}
$file .= '.php';
require_once $file;
//debug
//echo $file.'<br />';
}
}
This will look for class calls matching the 'Custom_' prefix
and reroute them to the relative location in this case
you only need to define the base prefix not the sub folders / classes
these will be auto detected by this code
APPPATH.'libraries/custom/example/example2.php'
You can call it in the controller the standard php way
$class = new Custom_Example_Example2;
or
$class = new custom_example_example2();
You can modify the script to your liking currently it expects all folders and filenames in the library to be lowercase but you can remove the strtolower() function to allow multiple casing.
you can change the require once to echo to test the output by uncommenting this line and refresh the page, make sure you have a class init / test in the controller or model to run the test
echo $file.'<br />';
Thanks
Daniel
In Pro.php
class Pro{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function showHelloWorld(){
return "Hello World";
}
}
In your controller
class Staff extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper(array('url_helper', 'url'));
$this->load->library("pro");
}
public function index() {
echo $this->pro->showHelloWorld();die;
}
}
Just do these things you can access your custom library in codeignitor.

Categories