How do I include file in codeIgniter application, which is outside application folder - codeigniter

I want to include this file
api/app/PHPexcel/phpexcel.php
Into this library
application/library/excel.php
In codeIgniter. Base url in my config file is empty.
pls help.

On your file try include or require
File-name: application > library > Excel.php
<?php
// dirname will let you access out side application folder.
$file = dirname(FCPATH . '/api/app/PHPexcel/phpexcel.php');
// $file = FCPATH . 'api/app/PHPexcel/phpexcel.php';
require_once $file;
class Excel {
public function __construct() {
}
}
Folder Layout
application
api
api > app
system
index.php
Best not to leave your base_url blank will run into some issues later.

Use absolute path. Something like:
'/var/www/html/api/app/PHPexcel/phpexcel.php'
or
'C:\wamp\www\api\app\PHPexcel\phpexcel.php'
Note that you have some predefined constants in CodeIgniter itself:
APPPATH => '/path/to/application/'
FCPATH => '/path/to/directory/where/index/php/file/is/
etc...
Those constants are defined in index.php file.
Commonly used practice is to put (e.g.) PHPexcel directory into
APPPATH . 'libraries'
location so structure could be
-application
--libraries
---PHPexcel
----phpexcel.php
---Excel.php # don't forget have it with ucfirst rule if CI3
or you can put directory in third_party and include it as
APPPATH . 'third_party/PHPexcel/phpexcel.php';
But it is up to you how do you like to arrange files/directories structure of your app.

In application/config/autoload.php file there is variable named $autoload['libraries'] = array();
you should define your library in this array and your library will be loaded automatically.

Related

Is there a way to get application name or print root folder name in a view

Is there a way to get the application name or print root folder name in a view in CodeIgniter?
Actually, I want to print the application in multiple views in CodeIgniter 3.
I tried with base URL but is not a perfect solution when you are using a framework.
FCPATH -> '/'
BASEPATH -> '/system/'
APPPATH -> '/application/'
APPPATH is your application folder
echo APPPATH;
if you need the root path then FCPATH.
echo FCPATH;
Following are the build-in constants you can use as per your requirements for getting the paths in Codeigniter:
EXT: The PHP file extension
FCPATH: Path to the front controller (this file) (root of CI)
SELF: The name of THIS file (index.php)
BASEPATH: Path to the system folder
APPPATH: The path to the “application” folder
Note:-
For More info regarding this
https://codeigniter.com/userguide3/general/reserved_names.html?highlight=apppath
Get application name or root folder name I used codeigniter reserved name in a php basename function.
echo FCPATH;
// output: E:\wamp64\www\MyApplicationName\
echo basename(FCPATH);
// output: MyApplicationName

unable to load header file through sub-folders in codeigniter

unable to load header file.
Structure of my codeigniter
Views->dashboard/admin_dashboard/dashboard.php
Controllers->dashboard.php
Now i have header in
Views->header/admin_header/header.php
I have tried with < ?php include('../../header/admin_header/header.php'); ?>
Thanks,
I think you shouldn't do include, try load view instead:
$this->load->view( 'header/admin_header/header' );
How about making subfolder for controller?
Here what you should do:
Ex:
controllers
- test
-- Main.php
Then you should regist it on config/routes.php, just add on newline:
$route['test/(:any)'] = 'test/$1';
Now you can access all controller files inside folder test

Is it ok to change public storage folder name from 'public/storage' to 'public/public'?

Laravel recommends here to create symlink from public/storage to storage/app/public. I'm looking for some inputs on whether renaming public/storage to public/public would cause any issues in other configurations or packages.
I am doing this because all my filenames in db are relative to storage/app folder. I store it that way since I can then uniformly retrieve any file using storage_path() helper. For example:
Protected file: storage/app/internal/secret.doc => filename in db = internal/secret.doc
Public file: storage/app/public/common.doc => filename in db = public/common.doc
Then to retrieve them:
// get filename 'internal/secret.doc' or 'public/common.doc'
$filename = DB::table('files')->find(1)->value('name'); // find file where id=1 and get only the name field
$file = storage_path('app/'.$filename);
This way I don't have to wonder whether I need to add public/ folder while retrieving a specific file. Also when I use asset() helper to generate urls to public files, again I can simply say:
$url = asset($filename)`;
Whereas, if I continue to use public/storage as the symlinked folder then I would have to do:
$temp_filename = str_replace('public/', '', $filename);
$url = asset('storage/'.$temp_filename);
Let me know if you have a different take or spot some issues with the renaming.
Once public/storage is sym linked to storage/app/public, you don't want your filepath in asset() to reference public. Public folder is already the root of your webserver, and asset() assumes public directory base. So your asset would be in asset("storage/$filename"). That actually pulls from storage/app/public/$filename.

How to get public directory?

I am a beginner here so pardon me for this question am using return File::put($path , $data); to create a file in public folder on Laravel. I used this piece of code from controller I need to know the value of $path how should it be.
Use public_path()
For reference:
// Path to the project's root folder
echo base_path();
// Path to the 'app' folder
echo app_path();
// Path to the 'public' folder
echo public_path();
// Path to the 'storage' folder
echo storage_path();
// Path to the 'storage/app' folder
echo storage_path('app');
You can use base_path() to get the base of your application - and then just add your public folder to that:
$path = base_path().'/public';
return File::put($path , $data)
Note: Be very careful about allowing people to upload files into your root of public_html. If they upload their own index.php file, they will take over your site.
I know this is a little late, but if someone else comes across this looking, you can now use public_path(); in Laravel 4, it has been added to the helper.php file in the support folder see here.
The best way to retrieve your public folder path from your Laravel config is the function:
$myPublicFolder = public_path();
$savePath = $mypublicPath."enter_path_to_save";
$path = $savePath."filename.ext";
return File::put($path , $data);
There is no need to have all the variables, but this is just for a demonstrative purpose.
Hope this helps,
GRnGC
I think what you are looking for is asset(''); You can use asset('storage'), after creating your symbolic link.
asset('public')
OR
url('public')

How require_once in joomla2.5

I Have a problem about require_once in joomla.
In this file php:
components\com_test\views\__test_r5\tmpl\default.php
I want to include some file using this code:
require_once (JPATH_ROOT.DS.'/includes/General.php');
but require_once does not works
The path you are trying to include will evaluate to something like: joomla//includes/General.php. Notice the double slashes before "includes".
The constant DS is defined to be a directory separator.
Try:
require_once (JPATH_ROOT.'/includes/General.php');
(without DS)
Try This :
require_once(JPATH_SITE.DS."includes/General.php");
The JPATH_SITE will return your physical path upto : installation folder.
also JURI::root() will return your site url
The best way to do this is to use #Fnatte variant.
Also have a look at the Joomla! constants definitions and adapt where needed.

Resources