How to download an uploaded image in PHP Codeigniter

流过昼夜 提交于 2019-12-11 15:16:34

问题


I have uploaded an image and then applied rotation operation on the image and fetch the flipped image in other view, now how to download that flipped image please guide me?

View code for uploading image:

<input type='file' name="userfile" size="20">
<input type="submit" name="upload" value="Flip">

Controller code for flip:

$data['title'] ='Flip Image';
$upload['upload_path'] = './assets/images/';
$upload['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload',$upload);
$filedata1 = $this->upload->data();
if(!$this->upload->do_upload('userfile')){
show_error($this->upload->display_errors());
}           
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path');
$config['rotation_angle'] ='hor';
$this->image_lib->initialize($config);
if(!$this->image_lib->rotate()){
    show_error($this->image_lib->display_errors());
}
$filedata = $this->upload->data();
$data['img2'] = base_url().'/assets/images/'.$filedata['file_name'];
print $this->load->view('pages/result',$data,true);

Download Function:

$this->load->helper('download');
$data = 'myimag.png';
force_download($img2, $data);
redirect('pages/result');

Now I am fetching this image result in other page and here I called the download function of controller(given above) on click the download button so that it should start downloading the file without asking the path for it but it is showing error: Result View:

<img src="<?php echo $img2;?>" width="200" height="200"> 
<?php echo form_open('pages/download()'); ?>
<input type="submit" name="submit" value="Download">
</form>

Error: Too few arguments to function Pages::download(), 0 passed in D:\xampp\htdocs\ImageTools\system\core\CodeIgniter.php on line 532 and exactly 1 expected


回答1:


Hope this will help you :

Either you can do like this :

Note : make sure here $img2 contains full path of image file

<img src="<?php echo $img2;?>" width="200" height="200"> 
<a href="<?php echo $img2;?>" download="myimage" >Download</a>

Or Simply do like this :

<img src="<?php echo $img2;?>" width="200" height="200"> 
<a href="<?=base_url('pages/download/'.$img2);?>" >Download</a>

And your download method should be like this :

function download($img2)
{
    $this->load->helper('download');
    /*make sure here $img2 contains full path of image file*/

    force_download($img2, NULL);
}

For more : https://www.w3schools.com/tags/att_a_download.asp



来源:https://stackoverflow.com/questions/51274893/how-to-download-an-uploaded-image-in-php-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!