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

标签 php mysql codeigniter pdf download

我对 CodeIgniter 比较陌生,但我可以掌握一两件事。 我当前面临一个问题,我希望管理员查看或下载用户注册时提交的上传文件(pdf 和 doc)。 一切正常,但我希望管理员下载文件而不仅仅是看到文件名。

这是我的 Controller :

<?php

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'];
        }
    }

}

这是我的 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>

这是我的输出的屏幕截图: I would like the pdf file or doc to be clickable

这是我设置 MySQL 的方法:

..just in case its not set correctly

我觉得它很小,但过去一周我的眼球很酸痛,我认为 Google 知道我的名字和我现在睡在哪里:)

提供的任何帮助都将是长期有效的。 谢谢

最佳答案

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

您可以像这样导入 Url Helper:

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

然后,您可以使用

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

base_url 方法将返回您的网站基本 URL,如配置文件中所指定。另外,该参数会将 URL 与您提供的字符串连接起来。这将是您文件的完整 URL。

关于php - 如何在 CodeIgniter 中将上传的文件(PDF、DOC 等)显示为可下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38610139/

相关文章:

php - 使用 Phalcon 生成 XML

php - 如何检查php文件是否被混淆?

php - 使用分页按类别列出项目

mysql - 选择时间戳 x 和时间戳 y 之间的数据,返回为 z 行数(缩小结果集)

mysql - 存储在 VARCHAR mysql 中的长字符串作为一行输出

php - 如何使用php将excel中的数据导入到mysql数据库中

php - 使用 codeigniter 中的连接事件记录获取数据库字段的总和

php - html 复选框遇到问题

php - 使用 PHP 和 MYSQL 对已知列进行搜索

javascript - 从 JQuery 调用 RESTFUL 服务