Welcome Guest, Not a member yet? Register   Sign In
Download Helper
#1

[eluser]elaniobro[/eluser]
I was reading through the forums trying to find a solution but was unable to do so.

I am trying to use an image as a trigger for downloading a file.

In my controller I have this:

Code:
<?php
    class Resume extends Controller
    {
        
        function Resume()
        {
            parent::Controller();
        }
    
        function index()
        {
            $info['title'] = 'Example | Resume';
            $info['page'] = 'resume';
            $info['query'] = $this->db->order_by('id ASC')->get('resume');  
    
            $this->load->helper('download');
            $data = @file_get_contents("com/resume.pdf");
            $name = 'com/elan_trybuch_resume.pdf';
            
        
                
            $this->load->view('header',$info);
            $this->load->view('campaign');
            $this->load->view('resume_view', $info);
            $this->load->view('footer');

    }
        

    }

?>

my view has the following:

Code:
<?php echo force_download($name, $data); ?>
<a href="&lt;?php echo force_download($name, $data); ?&gt;">&lt;?= img(array('src'=>'img/resume/resume_dl.gif','id'=>'resume-dl','height' => '', 'width' => '', 'alt' => 'Download Resume'))?&gt;</a>

Here is the error I am getting.

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: name

Filename: views/resume_view.php

Line Number: 22
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/resume_view.php

Line Number: 22
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: name

Filename: views/resume_view.php

Line Number: 23
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/resume_view.php

Line Number: 23
">
#2

[eluser]elaniobro[/eluser]
if I echo force_download() in the controller, this works, but not when I do it from the view.
#3

[eluser]helmutbjorg[/eluser]
You need to put the data into the info array like so:

Code:
$this->load->helper('download');
            $info['data'] = @file_get_contents("com/resume.pdf");
            $info['name'] = 'com/elan_trybuch_resume.pdf';
#4

[eluser]elaniobro[/eluser]
Thanks for the hint, I did such, however now when I navigate to that page, the download automatically happens. I tried moving it into it's own function, but that did not work either.

Then I decided to use the anchor() function to go to a new page, where I could echo the forced_download() inside the download function in the controller.

My only other question now resides with the anchor() function, how do you use this with an image instead of text?

Thanks for the help

Code:
&lt;?php
    class Resume extends Controller
    {
        
        function Resume()
        {
            parent::Controller();
        }
    
        function index()
        {
            $info['title'] = 'Example | Resume';
            $info['page'] = 'resume';
            $info['query'] = $this->db->order_by('id ASC')->get('resume');  
    
            
            $this->load->view('header',$info);
            $this->load->view('campaign');
            $this->load->view('resume_view', $info);
            $this->load->view('footer');

    }
        
        function download() //this is for a new page call.
        {

            $this->load->helper('download');
            $info['data'] = @file_get_contents("com/resume.pdf"); // Read the file's contents
            $info['name'] = 'com/resume.pdf';

            $this->load->view('header',$info);
            $this->load->view('campaign');
            $this->load->view('resume_view', $info);
            $this->load->view('footer');
            
        }
    }

?&gt;

anchor code:
Code:
&lt;?= anchor('/resume/download', 'download');?&gt;

I'll look it up in the wiki and scour the forums to see if there is any info about this.

Thanks!
#5

[eluser]elaniobro[/eluser]
I found this:
http://ellislab.com/forums/viewthread/97297/

And tried putting this in my syetem helper file:
Code:
/**
* @param    string   identical to the 1st param of anchor()
* @param    mixed    identical to the 3rd param of anchor()
* @param    string   the path to the image; it can be either an external one
*                    starting by "http://", or internal to your application
* @param    mixed    image attributes that have similar structure as the 3rd param of anchor()
* @return   string
*
* Example 1: anchor_img('controller/method', 'title="My title"', 'path/to/the/image.jpg', 'alt="My image"')
* Example 2: anchor_img('http://example.com', array('title' => 'My title'), 'http://example.com/image.jpg', array('alt' => 'My image'))
*/

function anchor_img($uri = '', $anchor_attributes = '', $img_src = '', $img_attributes = '')
{
    if ( ! is_array($uri))
    {
        $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
    }
    else
    {
        $site_url = site_url($uri);
    }

    if ($anchor_attributes != '')
    {
        $anchor_attributes = _parse_attributes($anchor_attributes);
    }
    
    if (strpos($img_src, '://') === FALSE)
    {
        $CI =& get_instance();
        $img_src = $CI->config->slash_item('base_url').$img_src;
    }
    
    if ($img_attributes != '')
    {
        $img_attributes = _parse_attributes($img_attributes);
    }

    return '<a href="'.$site_url.'">'.'<img src="'.$img_src.'" />'.'</a>';
}

Now when I put this:
Code:
&lt;?= anchor_img('/resume/download',array('src'=>'img/resume/resume_dl.gif','border'=>'0'),'testing image') ?&gt;

Nothing is happening. Tongue Any ideas?
#6

[eluser]elaniobro[/eluser]
this appears to work:
Code:
anchor('test',img(array('src'=>'admin/images/unpublish.png','border'=>'0','alt'=>'testing image')),array('class'=>'imglink'));

However, I would like to go a step further and do rollovers, tried the following but with no luck:
Code:
&lt;?= anchor('/thoughts/more/'.$row->id,img(
array('src'=>'img/thoughts/readmore_a.jpg',
      'border'=>'0','alt'=>'Read More'),
array('onmouseover'=> 'readMore.src='base_url().'img/thoughts/readmore_b.jpg";',
      'onmouseout'=>'readMore.src='base_url().'img/thoughts/readmore_a.jpg";',
      'id' =>'readmore')));?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB