How to check view exist or not? - php

In CodeIgniter I am trying to check before loading the view that the file exist or not from controller, I tried like-
public function foo($file = "")
{
if (file_exists(APPPATH."views/log/{$file}.".EXT))
{
$this->load->view('log/'.$file);
}
else
show_404();
}
But the file is exist there then also the file_exists() method is returning false, I have checked it using var_dump() also. I am not getting what is the problem here, I am guessing that there is something problem related to mapping because the directory structure is like-
application
|--controllers
| |--ctrl.php //Controller file where I am checking
|--views
|--log
|-- .. //Checking files for here
but how can I resolve?
Edit: Just mistyped 'views' to 'view'(in code where I am calling file_exists() method in if) corrected it.

The EXT constant in Codeigniter is equal to .php and defined in index.php:
// The PHP file extension
// this global constant is deprecated.
define('EXT', '.php');
You are adding an extra dot:
APPPATH."views/log/{$file}.".EXT
// ^
Which would result in something like views/log/myfile..php
Since the constant is being phased out you should avoid it.

Related

Laravel: check file existence without extension

I'm saving files in server only with a md5 hashed name without extension.
I noticed laravel Storage::exists() method will return true even if file does not exists. in fact laravel assume file name as a directory name. is there any way to force laravel to check file existence ?
Actually, it's because the way PHPs file_exists function works. Anyways, you may use something like this:
if(is_file('68e109f0f40ca72a15e05cc22786f8e6')) {
// ...
}
Also, you may try glob like this:
$all = glob('*'); // Read everything from current directory in array
if(in_array('68e109f0f40ca72a15e05cc22786f8e6', $all)) {
// ...
}
There are more ways to do this but why not use an extension?

What are possible reasons for bonfire Assets::add_module_css to not include file?

I have a primary controller located in secure/application/modules/gps/controllers that has a constructor that looks like this:
public function __construct()
{
parent::__construct();
$this->load->model('gps_model');
Assets::add_module_js('gps', 'gps.js');
Assets::add_module_css('gps','gps.css');
if($this->input->get('clear') != false){
$this->session->sess_destroy();
}
}
My CSS file that I am trying to include is located in the folder secure/application/modules/gps/assets/css. The code executes fine without warning, but the CSS file does not get included for any methods. Is there a configuration setting the may override the assets directory, or is there some other reason it's not being found/added? (The JS file is not being added either. The bonfire base CSS files (screen.css) IS getting loaded fine.
We found the solution to our particular problem.
The assets/cache directory did not exist. Once the server could write (it must exist and be writeable!) to the [document_root]/bonfire/public/assets/cache directory all was good.
This is not a solution, but I have faced the same problem and found help with the below information.
It'll added in your page but can you just look on ctrl+u source where bonfire will auto rename your file.
For example : In my code I have added id_proof_master.css file like below.
public function __construct()
{
parent::__construct();
$this->auth->restrict($this->permissionView);
$this->load->model('id_proof_master/id_proof_master_model');
$this->lang->load('id_proof_master');
$this->form_validation->set_error_delimiters("<span class='error'>", "</span>");
Template::set_block('sub_nav', 'master/_sub_nav');
Assets::add_module_js('id_proof_master', 'id_proof_master.js');
Assets::add_module_css('id_proof_master', 'id_proof_master.css');
}
And it's working fine but when I have check in source view (ctrl+u) it will show file name like "id_proof_master_master_mod.min.css" so can you just check it out in source; maybe it'll show you with some other name like my file.

PHP Function Scope Failure

I am struggling to understand scope and what's preventing my new code from working (assuming it is a scope issue).
The following function is in a file PATH.'/includes/custom-functions.php' that references a class:
function infusion() {
require_once(PATH.'/classes/infusion.php'); //PATH is defined in WordPress from ~/wp-content/themes/theme/
return new infusion();
}
The class is reliant on PATH.'/api/isdk.php' and connection credentials from another file within /api/ directory. From within PATH .'/includes/custom-functions.php', I have many other functions that call $infusion = infusion(); and work perfectly.
PROBLEM
I have created a new file: PATH.'/includes/report.php' which I need to access $infusion = infusion();but can't get to work by either repeating the function infusion() definition from above; using require_once();; or using include();. All 3 of those options simply kill the rest of the code and I can only come to the conclusion - well, I have no conclusion.
Any help would be greatly appreciated.
I'm assuming the code isn't using namespaces, therefore you aren't permitted to redeclare the infusion function (either by redefining the function, or re-including the class).
Your includes/report.php file should simply have:
require_once PATH.'/includes/custom-functions.php';
// your other code here ...
$infusion = infusion();
It may be the case that other files / classes that you're including in your file are already requiring custom-functions.php along the line, so you may be able to skip that entirely. Also note that the PATH constant should have already been defined somewhere (either directly or via an included file) before you attempt to use it. If you set your error_reporting to include E_ALL, you'll get a notification in your error log if that constant doesn't exist.
If that fails, your error log(s) may provide some additional background on what your issue is.

Unable to access library function in codeigniter

I am noob in codeigniter. I used an external class file for getting the length of an audio file. I put the file in application/libarary directory. After googling and reading documentation I tried two things for accessing the libarary function which I have mentioned under the codeigniter problem.
Working Demo from the External Library File
require ('classAudioFile.php');
$filename = "40f11852c2314cdf1d06308f07de3b1c.mp3";
$AF = new AudioFile;
$AF->loadFile($filename);
echo $AF->wave_length;
CodeIgniter Problem
$filename = "./uploads/audio_files/347f21502e9b27fac4707b07c3c15eb7.mp3";
$CI =& get_instance();
$CI->load->library('AudioFile');
$CI->AudioFile->loadFile($filename);
$filename = "./uploads/audio_files/347f21502e9b27fac4707b07c3c15eb7.mp3";
$this->load->library('AudioFile');
$this->AudioFile->loadFile($filename);
I am getting Call to a member function loadFile() on a non-object in D:\wamp\www\webcartz\application\modules\playlist\controllers\admin.php on line 149 this is the actuall error.. can any body please help. I tried to call it in both Controller and View file.
to normalize everything try the following:
the AudioFile.php you put in the library folder, make the file name all lower case, like audiofile.php. if it is called classAudioFile.php, rename it to simply audiofile.php
check that there is a class AudioFile in audiofile.php.
change the AudioFile in the load->library to all lowercase
change the AudioFile in the $this->AudioFile-> to all lowercase
if it still does not work change the name of the class (not the file) to Audiofile, capital A, lower case f.
Also, there should be an default - no parameter constructor in the class Audiofile. like this:
public function __construct()
{
// you can put some code here if you want
}

Is there a way to get the name of the top level PHP file from inside an included one?

Let's say I'm writing a global logData method that wants to write to a log file that has the same name as the php that's running it, but with a .log extension.
I'm including this logging in a parent php with the intention of having it always write to log files that are whatever the *parent file name is (not the tools.php lib in which it's sitting).
So, I have
/some/arbitrary/directory/parent.php
which calls
include ("/path/to/my/php/libs/tools.php");
but when I run my logging method that's in tools.php it logs to a file called
/path/to/my/php/libs/tools.php.log
rather than
/some/arbitrary/directory/parent.php.log (which is what I'd like).
I'm using __FILE__ which is behaving this way (probably as its intended to). Is there a command for getting the parent's file name so that I can get this to work as I intend? Or will I have to pass FILE as a param into my method from the parent php to get it to write to the correct output file?
TIA
You could probably use $_SERVER["SCRIPT_FILENAME"]
debug_backtrace() will give you what you need.
http://php.net/manual/en/function.debug-backtrace.php
You need to pass __FILE__ to the log class.
Something like:
// file:/some/arbitrary/directory/parent.php
$logger = new Logger(__FILE__);
// file:/path/to/my/php/libs/tools.ph
public function __construct($file) {
// But it is not good idea to save log file same with php files.
$this->log_path = $file.'.log';
}

Categories