0

I'm trying to put links to download images on my website and I also want to show the download size near the link. I'm using

<?php
$filename = base_url().'carteles/as_cartel-8.jpg';
echo filesize($filename);
?>

but I get a PHP error. I think it's because "base_url" cannot be used there. But I don't know how to get the url for filesize (which is inside my server) to manage it.

Edit:

The error is:

A PHP Error was encountered

Severity: Warning

Message: filesize(): stat failed for http://localhost/TCE/Festivales/carteles/as_cartel-8.jpg

Filename: views/categorias_v.php

Line Number: 32

Backtrace:

File: C:\xampp\htdocs\TCE\Festivales\application\views\categorias_v.php
Line: 32
Function: filesize

File: C:\xampp\htdocs\TCE\Festivales\application\views\template\template.php
Line: 5
Function: view

File: C:\xampp\htdocs\TCE\Festivales\application\controllers\categorias_c.php
Line: 22
Function: view

File: C:\xampp\htdocs\TCE\Festivales\index.php
Line: 315
Function: require_once

I do have autoload configured, I mean, base_url works fine when I try to do other stuff, my problem is when I try to use "filesize".

Lenita
  • 11
  • 1

2 Answers2

1

In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php

$autoload['helper'] = array('url');

the to debug it, you should do see what baseurl return first. then make sure the complete link its correctly access to the images

echo base_url();

good luck

Evinn
  • 153
  • 1
  • 11
0

I believe the problem is due to you trying to open the link, rather than the file itself. Try using CodeIgniter's File Helper to get the file size. The helper has a function, get_file_info(), that will help you.

If you were doing this in the controller, it would look something like:

$this->load->helper('file');
$file = APPPATH . 'carteles/as_cartel-8.jpg';
$fileinfo = get_file_info($file);
$data['filesize'] = $fileinfo['size'];

However, if you want to load the File Helper in a view, you will have to load the helper a bit differently. Here is some info on that: CodeIgniter helper in the view. Hope this helps your cause.

Community
  • 1
  • 1
cfnerd
  • 3,658
  • 12
  • 32
  • 44