简体   繁体   English

如何在CodeIgniter中将上传文件(PDF,DOC等)显示为可下载链接

[英]How to Display uploaded files (PDF, DOC etc) as downloadable link in CodeIgniter

I am relatively new to CodeIgniter but I can grasp a thing or two. 我对CodeIgniter相对较新,但是我可以掌握一两个东西。 I am currenly facing a problem where I want the admin to view or download uploaded files (pdf and doc) submitted upon user registration. 我目前正面临一个问题,我希望管理员查看或下载在用户注册后提交的上传文件(pdf和doc)。 Everything is working well but I would wish the admin to download the files not just see the filename. 一切工作正常,但我希望管理员下载文件而不仅仅是看到文件名。

Here is my Controller: 这是我的控制器:

<?php

class NominationController extends CI_Controller { 类NominationController扩展了CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->model('Nomination');
    $this->load->library('session');
    }
/*
function for manage Nomination.
return all Nominations.
created by your name
created at 25-07-16.
*/
public function manageNomination() { 

    $data["Nominations"] = $this->Nomination->getAll();
    $this->load->view('Nomination/manage-Nomination', $data);

}
/*
function for  add Nomination get
created by your name
created at 25-07-16.
*/
public function addNomination() {

    $this->load->view('Nomination/add-Nomination');

}
/*
function for add Nomination post
created by your name
created at 25-07-16.
*/


public function addNominationPost() {
                    $data['title'] = $this->input->post('title');
                            $data['first_name'] = $this->input->post('first_name');
                            $data['last_name'] = $this->input->post('last_name');
                            $data['email_id'] = $this->input->post('email_id');
                            $data['phone_no'] = $this->input->post('phone_no');
                            $data['company'] = $this->input->post('company');
                            $data['popularity'] = $this->input->post('popularity');
                            $data['nominee_type'] = $this->input->post('nominee_type');
                            $data['innovation_name'] = $this->input->post('innovation_name');
                            $data['organisation_type'] = $this->input->post('organisation_type');
                            $data['category'] = $this->input->post('category');
                            $data['date_of_innovation'] = $this->input->post('date_of_innovation');
                            $data['company_offices'] = $this->input->post('company_offices');
                            $data['need_addressed'] = $this->input->post('need_addressed');
                            $data['user_benefits'] = $this->input->post('user_benefits');
                            $data['uniqueness'] = $this->input->post('uniqueness');
                            if ($_FILES['supporting_documents']['name']) { 
        $data['supporting_documents'] = $this->doUpload('supporting_documents');
     } 
                    $this->Nomination->insert($data);
    $this->session->set_flashdata('success', 'Nomination added Successfully');
    redirect('success');
}
/*
function for edit Nomination get
returns  Nomination by id.
created by your name
created at 25-07-16.
*/
public function editNomination($Nomination_id) {
    $data['Nomination_id'] = $Nomination_id;
    $data['Nomination'] = $this->Nomination->getDataById($Nomination_id);
    $this->load->view('Nomination/edit-Nomination', $data);
}
/*
function for edit Nomination post
created by your name
created at 25-07-16.
*/
public function editNominationPost() {

    $Nomination_id = $this->input->post('Nomination_id');
    $Nomination = $this->Nomination->getDataById($Nomination_id);
                    $data['title'] = $this->input->post('title');
                    $data['first_name'] = $this->input->post('first_name');
                    $data['last_name'] = $this->input->post('last_name');
                    $data['email_id'] = $this->input->post('email_id');
                    $data['phone_no'] = $this->input->post('phone_no');
                    $data['company'] = $this->input->post('company');
                    $data['popularity'] = $this->input->post('popularity');
                    $data['nominee_type'] = $this->input->post('nominee_type');
                    $data['innovation_name'] = $this->input->post('innovation_name');
                    $data['organisation_type'] = $this->input->post('organisation_type');
                    $data['category'] = $this->input->post('category');
                    $data['date_of_innovation'] = $this->input->post('date_of_innovation');
                    $data['company_offices'] = $this->input->post('company_offices');
                    $data['need_addressed'] = $this->input->post('need_addressed');
                    $data['user_benefits'] = $this->input->post('user_benefits');
                    $data['uniqueness'] = $this->input->post('uniqueness');
                    if ($_FILES['supporting_documents']['name']) { 
        $data['supporting_documents'] = $this->doUpload('supporting_documents');
        unlink('./uploads/Nomination/'.$Nomination[0]->supporting_documents);
    } 
            $edit = $this->Nomination->update($Nomination_id,$data);
    if ($edit) {
        $this->session->set_flashdata('success', 'Nomination Updated');
        redirect('success');
    }
}
/*
function for view Nomination get
created by your name
created at 25-07-16.
*/
public function viewNomination($Nomination_id) {
    $data['Nomination_id'] = $Nomination_id;
    $data['Nomination'] = $this->Nomination->getDataById($Nomination_id);
    $this->load->view('Nomination/view-Nomination', $data);
}
/*
function for delete Nomination    created by your name
created at 25-07-16.
*/
public function deleteNomination($Nomination_id) {
    $delete = $this->Nomination->delete($Nomination_id);
    $this->session->set_flashdata('success', 'Nomination deleted');
    redirect('manage-Nomination');
}
/*
function for activation and deactivation of Nomination.
created by your name
created at 25-07-16.
*/
public function changeStatusNomination($Nomination_id) {
    $edit = $this->Nomination->changeStatus($Nomination_id);
    $this->session->set_flashdata('success', 'Nomination '.$edit.' Successfully');
    redirect('manage-Nomination');
}
    /*
function for upload files
return uploaded file name.
created by your name
created at 25-07-16.
*/
public function doUpload($file) {
    $config['upload_path'] = './uploads/Nomination';
    $config['allowed_types'] = '*';
    $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload($file))
        {
          $error = array('error' => $this->upload->display_errors());
          $this->load->view('upload_form', $error);
        }
        else
        {
          $data = array('upload_data' => $this->upload->data());
          return $data['upload_data']['file_name'];
        }
    }

} }

Here is my View.php 这是我的View.php

<tr>
                    <td width='30%' class='label-view text-right'>
                    <?php echo SiteHelpers::activeLang('Uniqueness', (isset($fields['uniqueness']['language'])? $fields['uniqueness']['language'] : array())) ;?>
                    </td>
                    <td><?php echo $row['uniqueness'] ;?> </td>

                </tr>

                <tr>
                    <td width='30%' class='label-view text-right'>
                    <?php echo SiteHelpers::activeLang('Supporting Documents', (isset($fields['supporting_documents']['language'])? $fields['supporting_documents']['language'] : array())) ;?>
                    </td>
                    <td><?php echo $row['supporting_documents'] ;?></td>
                </tr>

Here is the screenshot of my output: I would like the pdf file or doc to be clickable 这是我的输出的屏幕截图: 我希望pdf文件或doc可点击

Here is how I have set my MySQL: 这是我设置MySQL的方法:

..just in case its not set correctly ..以防万一其设置不正确

I feel its something small but my eyeballs are sore for the past week and I think Google knows my name and where I sleep by now :) 我感觉有点小,但是过去一周我的眼球很痛,我认为Google知道我的名字和我现在在哪里睡觉:)

Any help accorded will go a long away. 给予的任何帮助都将走很长一段路。 Thank you 谢谢

According to the documentation: https://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url 根据文档: https : //www.codeigniter.com/user_guide/helpers/url_helper.html#base_url

You can import the Url Helper like this: 您可以像这样导入网址帮助器:

$this->load->helper('url');

Then, you can use 然后,您可以使用

<a href="<?= base_url('path/to/directory/' . $row['supporting_documents']); ?>"> Download </a>

The method base_url will return your site base URL, as specified in your config file. 方法base_url将返回配置文件中指定的站点基本URL。 Plus, the argument will concatenate that URL with the string you supplied. 另外,该参数会将该URL与您提供的字符串连接在一起。 That will be the complete URL of your file. 这将是文件的完整URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM