unable to load header file through sub-folders in codeigniter - 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

Related

codeigniter direct download inside application directory

I have a directory called storage inside codeigniter application directory. I want to create direct download to files in this directory. What i currently do is the below function but this isn't direct download.
function download_file($id){
$this->load->helper('download');
$data = file_get_contents(APPPATH . 'storage/videos/'.$id.'.mp4'); // Read the file's contents
force_download($id.'.mp4', $data);
}
Some of these files are gigabytes, how do i generate a direct link to these files?
See the code for creating link for direct download
<a href = "<?= base_url('storage/videos/'.$id.'.mp4') ?>" download ><?= $id.'.mp4' ?></a>

How to include js file from 'resources' folder (Laravel 5.5)

I used the following code to include my js file:
Meta::addJs('admin_js', '/resources/assets/admin_js/admin_app.js');
The file exists but in the console I see status 404.
If I move the file to 'public' folder - all ok. But I want that this file be stored in 'resources' directory
you have to move js files to public or store into storage and make symlinks.
or you need to create symlinks the resources directory to a public directory(which is not recommended).
you need to use the most recommended and effective method of Laravel of using Laravel mix. Please use the link below to read about laravel mix a solution.
https://laravel.com/docs/5.6/mix
It will allow you to place your js assets into resources directory and make compressed js file in public which will be used by the setup.
Update for LARAVEL 7.0 :
Adding a vanilla JavaScript file to ‘resources’ :
Create the file in resources/js (called 'file.js' in this example)
Go to webpack.mix.js file (at the bottom)
Add there: mix.js('resources/js/file.js', 'public/js');
Run in terminal : npm run production
i don't know if i am late but when i needed to load js file from directories and sub directories in my view file, i did this and it worked perfectly for me.
BTW i use laravel 5.7.
first of all i wrote a function that searched for any file in any given directory with this
.
/**
* Search for file and return full path.
*
* #param string $pattern
* #return array
*/
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
the above will return a full path for each ".js" file in my "resources/view" directory.
Then i made a call to the above function to copy the content of each js to a single js file (new.js) which i created in my public/js using the below function.
/**
* Load js file from each view subdirectory into public js.
*
* #return void
*/
function load_js_function(){
//call to the previous function that returns all js files in view directory
$files = rglob(resource_path('views').'/*/*.js');
foreach($files as $file) {
copy($file, base_path('public/js/new.js'));
}
}
After this i made call to the load_js_function() in my master blade layout(where i load all my css,js etc) immediately after loading the public/js/new.js, you can see below.
<script src="{!! asset('js/new.js') !!}"></script>
<!-- load all js from each module -->
{{ load_js_function() }}
These solution updates the file in public as you update the content of the original file.
Vote up if it works for you and comment if you have an issue implementing this, i will be glad to shed more light.
cheers

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

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.

CakePHP - need URL to route to the tmp directory?

I am trying to create thumbnail pics using GD lib in Cake PHP.
I can write the resized thumbnail to the tmp directory, and create the sensible URL to show the image from the tmp directory:
//set up empty GD resource
$tmp = imagecreatetruecolor(200*$iw/$ih, 200);
//create thumbnail image in the empty GD resource
imagecopyresampled($tmp, $src, 0, 0, 0, 0,200*$iw/$ih, 200, $iw, $ih);
//build full path to thumbnail in the cakephp tmp directory
$thumbfile = APP.'tmp/thumb.jpg';
//build URL path to the same thumbnail
$thumblink = $this->webroot.'tmp/thumb.jpg';
//write jpeg to the tmp directory
$return=imagejpeg($tmp,$thumbfile);
//write out the image to client browser
echo "<img=\"$thumblink\" alt=\"thumb\" height=\"200\" width=\"200*$iw/$ih\">";
The thumbnail gets created and written to the tmp directory, but when I try to access the URL I get the following error message:
Error: TmpController could not be found.
Error: Create the class TmpController below in file: app/Controller/TmpController.php
Obviously I have a routing error - Cake tries to call the tmp controller, in stead of looking in the tmp direcectory. How can I fix this, or is there an alternative way to serve temporary thumbnails using GD lib?
I am planning to create unique thumbnails per session or user, but I need to get this working first.
Routing in Config/routes.php:
Router::connect('/', array('controller' => 'MsCake', 'action' => 'index'));
Router::connect('/pages/*', array('controller' => 'pages'));
CakePlugin::routes();
I looked at ThumbnailHelper, but that doesn't use GD Lib. I also need to be able to access files stored on non-apache accessible directories from outside, but I can't even access any temporary symbolic links to get to them. eg.
create a temporary symbolic link in the tmp directory, pointing to the file in question.
create a HTML link, pointing to the symbolic link using $this->webroot.'tmp/link-to-myfile', as above
...and I get the same error as above - Error: TmpController could not be found.
Don't do that
If you do anything to make files in the tmp dir web-accessible - you're severely lowering your site's security. Things in the tmp directory are never supposed to be web accessible.
Put your images in the webroot
A better idea is to put your temporary images in the webroot directory - which is the only directory that is ordinarily web accessible. For example:
$filename = md5($userId);
$thumbfile = APP.'webroot/img/cache/' . $filename . '.jpg';
...
$url = '/img/cache/' . $filename . '.jpg';
Or route to a controller action
Alternatively, route to a controller action to handle the request using the media view class. Note however though, that serving images with php is not free - there can be a noticable delay while your request is processed - where'as pointing at a static file does not have this cost/risk since it's just the webserver taking care of serving the content.
Since it's temporary, what you could do is to display the image as a data url instead of to the tmp directory, like so (replace from after imagecopyresampled() call):
ob_start();
imagepng($tmp);
$contents = ob_get_contents();
ob_end_clean();
imagedestroy($tmp);
//write out the image to client browser
echo "<img src='data:image/png;base64,".base64_encode($contents)."' alt='thumb' height='200' width='".(200*$iw/$ih)."'>";
This uses a bit more bandwidth since the image is base64 encoded rather than sent as binary.

Prevent direct access of downloaded file in CI

How to prevent direct access to my download file using CodeIgniter.
Supposed I stored all of my file in
application/secretdirectory/file1.jpg
application/secretdirectory/file2.text
application/secretdirectory/file3.zip
usually, I create direct link to access these files.
<a href="application/secretdirectory/file3.zip" > download here </a>
I want to prevent it, I want my link will be
<a href="<? echo site_url() . "download/file3.zip" ?>" > download here </a>
can you help me ???
It's probably a good idea to pass it to a controller like this:
Class File extends Controller{
function download($file){
$data = file_get_contents('path/to/' . $file); // Read the file's contents
$name = $file;
force_download($name, $data);
}
}
Your link would be:
<?php echo anchor('file/download/' . $thefile, 'Download');?>
This way they'd never know which directory it came from.
I'm sure there is a library in CI to assist with this
basically you send headers for the correct MIME type, the content length (filesize()) and then output to the browser to download echo file_get_contents() may work)
If using Apache, you'll also want to deny showing directoy content.
<Files .*>
Order Deny,Allow
Deny From All
</Files>

Resources