codeigniter direct download inside application directory - codeigniter

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>

Related

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

Creation of zip folder in php

I am trying to create a zip folder of my downloaded images.
Here is my code. I am not getting any errors, but the zip is not getting downloaded.The code is getting compiled and I am getting the output till the display part of the current directory, but after that the code seems to go wrong somewhere and I am not able to get any Zip archive.
<?php
$conn_id=ftp_connect("localhost") or die("Could not connect");
ftp_login($conn_id,"kishan","ubuntu"); //login to ftp localhost
echo "The current directory is " . ftp_pwd($conn_id); //display current directory
ftp_chdir($conn_id,'/var/www/test1'); //changing to the directory where my images are downloaded.
echo "<br/><p> Changing to directory" . ftp_pwd($conn_id);
$file_folder=".";
echo "<br/> The content of the directory is <br/>";
print_r(ftp_rawlist($conn_id,".")); // display the contents of the directory
if(extension_loaded('zip')) //check whether extension is loaded
{
$zip=new ZipArchive();
$zip_name="a.zip"; //some name to my zip file
if($zip->open($zip_name,ZIPARCHIVE::CREATE)!==TRUE)
{
$error="Sorry ZIP creation failed at this time";
}
$contents=ftp_nlist($conn_id,".");
foreach($contents as $file) //addition of files to zip one-by-one
{
$zip->addFile($file_folder.$file);
}
$zip->close(); //seal the zip
}
if(file_exists($zip_name)) //Content-dispostion of my zip file
{
header('Content-type:application/zip');
header('Content-Disposition:attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}
?>
I just found that there is something wrong with the data that is passed to the for loop.
$contents=ftp_nlist($conn_id,".");
foreach($contents as $file) //addition of files to zip one-by-one
{
$zip->addFile($file_folder.$file);
}
I am not sure exactly where the thing is going wrong but here is the update. What I did was write markup using html and then passing the files to this php script via a POST method, as
foreach($post['files'] as $file){
$zip->addFile($file_folder.$file);} // Adding files into zip
This code is absolutely working fine, but now I need to figure out why post method is working and compressing my files whereas my normal for looping over files is not!!

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.

codeigniter image path

I am a codeignite newbie and tried to add an image in my file under view folder.
I add <img src="../images/myImage.jpg"></img> to my service_view.php. All I can see is a broken link of a small icon.
however, if I change my path to
<img src="../../user_guide/images/myImage.jpg"></img>
I can see the image.
My file system is as follow:
application-
view ->folder (where service_view.php is located)
images -> folder (where myImage.jpg is located)
user_guide-
images ->folder ((where myImage.jpg is located))
Can anyone help me about this? Thanks a lot!
I would suggest storing images in an images folder at the same level as the application folder, instead of trying to mix both PHP code and resources underneath application. Then just link to it like you did to the user guide:
<img src="../../images/myImage.jpg" />
I know more than year has passed since the question was asked but for anyone who comes looking in future, Here is my solution....
I've tried this created a folder images at same level as that of application, in main index.php added a line before last require_once statement.
define('IMAGE_PATH', APPPATH.'../images/');
and used IMAGE_PATH anywhere I wanted to display images.
Hope this helps
I just have an image folder,
domain.com/images
then on the views i just use /images
<img src="<?php echo base_url(); ?>images/image.png" />
and put the images folder outside of the application folder in the same directory
Create an images folder at the same level of the applications folder.
With base_url() you can get the location of your site's base (cfr config).
This is useful because unlike site_url(), you can supply a string to a file, such as an image or stylesheet.
For example:
<img src="<?php echo base_url("/images/icons/stackOverflow.png"); ?>" alt="StackOverflow" title="StackOverflow is the best!" />
This would give you something like: ... src="http://example.com/images/icons/stackOverflow.png" ...
Source: http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
it is better practice to upload the images in a folder that is present in the root or the site folder and use base href in the first line of head tag. then the whole page does not need and http path reference. just use the relative path. as codeigniter run using the index.php file in the root, we should use all the relative path w.r.t that index.php page
on local server try this
<img src="http://localhost/ci/uploads/<?php echo $this->upload->data()['file_name'];?>" width="200px"/>
on live use this
<img src="<?php echo base_url();?>/uploads/<?php echo $this->upload->data()['file_name'];?>" width="200px"/>
put a folder on your root directory such as "assets/images/" and the in your code just use it as
<img src="assets/images/imgname.jpg"></img>
Your file structure should be
-applications
-images
-assets
-Or any folder.
Keep the application folder, images folder, and assets folder inside the basic root folder.
Use when you want to include something from your root folder
FCPATH = C:\xampp\htdocs\your_root_folder\
<img src="<?php echo FCPATH; ?>/images/myImage.jpg" />
<img src="<?php echo base_url('/assests/images/image_name.jpg')?>" alt="">
This is the exact path for codeigniter, don't need write the "../../.." just write the path from assets (folder name)(sibling of application).
base_url gives the path of your project folder, not the path of the view folder inside application folder.

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