简体   繁体   English

如何在Codeigniter中调用带有参数的函数以强制下载

[英]How to call function with parameter in view to Force Download in codeigniter

I want to call function download when I click the Download Text, but my href always showing php error. 当我单击下载文本时,我想调用函数下载,但是我的href始终显示php错误。 I got stuck here and confuse how to do it. 我被困在这里,混淆了如何做。

This is my view : 这是我的看法:

<?php $i=1; foreach($map as $dokumen)
{
?>
    <tr>
    <th scope="row"><?php echo $i?></th>
    <td><?php echo $dokumen?></td>
    <td><?php $path = 'file/dokumenkp/' . $dokumen;?>
    <a href="<?php echo base_url(). 'dokumenkp/downloadfiles' . $path['path']; ?>">Download</a>
    </td>
    </tr>
<?php $i++?>
<?php
}?>

And this is my function in Controller : 这是我在Controller中的功能:

public function downloadfiles($path)
{
    force_download($path, NULL);
}

Try Below example And provide full path using getcwd() 尝试以下示例,并使用getcwd()提供完整路径

public function downloadfiles($path){

            $name = 'YourFileName';
            $path = getcwd().$path;
            $mime = get_mime_by_extension(getcwd().$path);
            header('Pragma: public');     // required
            header('Expires: 0');         // no cache
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
            header('Cache-Control: private',false);
            header('Content-Type: '.$mime);  // Add the mime type from Code igniter.
            header('Content-Disposition: attachment; filename="'.basename($name).'"');  
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: '.filesize($path)); // provide file size
            header('Connection: close');
            readfile($path); // push it out
            exit();
}

It seems to me there are a couple syntax errors. 在我看来,有一些语法错误。 The main one is in these lines. 主要的是在这些行中。

<td><?php $path = 'file/dokumenkp/' . $dokumen;?>
<a href="<?php echo base_url(). 'dokumenkp/downloadfiles' . $path['path']; ?>">Download</a>

In the first line, $path is defined and is a string, but in the second line it is treated as an array ie. 在第一行中, $path被定义并且是一个字符串,但是在第二行中,它被视为一个数组,即。 $path['path'] . $path['path'] That's going to produce an error. 这将产生一个错误。

But there is still a problem if, in the second line, $path is used as the string it is, ie 但是,如果在第二行中将$path用作字符串,则仍然存在问题,即

<a href="<?php echo base_url(). 'dokumenkp/downloadfiles' . $path; ?>">Download</a>

The problem is the lack of a directory separator between 'dokumenkp/downloadfiles' and $path . 问题是'dokumenkp/downloadfiles'$path之间缺少目录分隔符。

Here is code that fixes all the issues. 这是解决所有问题的代码。 It is also refactored a little bit to be, IMHO, a little cleaner. 它也被重构为恕我直言,更清洁。 (Note how you can use "pre-incrementing" on the counter $i before it is echoed.) (请注意如何在回显之前在计数器$i上使用“预递增”。)

<?php 
$i=0; 
foreach($map as $dokumen)
{
    $path = 'file/dokumenkp/' . $dokumen;
?>
    <tr>
        <th scope="row"><?php echo ++$i; ?></th>
        <td><?php echo $dokumen; ?></td>
        <td><a href="<?php echo base_url('dokumenkp/downloadfiles/' . $path); ?>">Download</a></td>
    </tr>
    <?php 
} ?>

When you get to the controller method downloadfiles($path) several things have to be accomplished to make $path be what you need. 当您到达控制器方法downloadfiles($path) ,必须完成几件事才能使$path成为您所需要的。

The first problem is that $path is a string with a series of '/' characters in it. 第一个问题是$path是一个带有一系列'/'字符的字符串。 CodeIgniter is going to interpret each item after a slash as another argument to the function. CodeIgniter会将斜线后的每个项目解释为该函数的另一个参数。 In other words, each slash-separated segment of $path will become another variable. 换句话说, $path每个斜杠段将成为另一个变量。 There are a couple of ways to deal with this fact. 有两种方法可以处理此事实。

This problem would not exist if we sent only the file's name - eg downloadfiles('Pedoman.pdf') - where the path could be prepended to the file name. 如果仅发送文件名,例如downloadfiles('Pedoman.pdf') ,则该问题将不存在。 But that would require that all downloads come from the same folder. 但这将要求所有下载都来自同一文件夹。 Your requirement is for different download source folders so we will have to convert $path back into a useful string. 您的要求是使用不同的下载源文件夹,因此我们将必须将$path转换回有用的字符串。

That's not very hard to do, but the parts of the framework we will use don't really require that any input parameters be defined for the function downloadfiles() . 这并不是很困难,但是我们将使用的框架部分实际上并不需要为downloadfiles()函数定义任何输入参数。 So we won't even though we will still send some. 因此,即使我们仍然会发送一些邮件,我们也不会。

The next issue is that the force_download() function requires an absolute path (full path from the root of the drive). 下一个问题是force_download()函数需要绝对路径(从驱动器根目录开始的完整路径)。 Assuming that the folder "file" is at the same level as "application" we can use FCPATH as the starting place. 假设文件夹“文件”与“应用程序”处于同一级别,我们可以将FCPATH用作起始位置。 ( FCPATH is the full path to the folder where CodeIgniter's index.php is found.) FCPATH是找到CodeIgniter的index.php的文件夹的完整路径。)

Here's a version of downloadfiles() that accomplishes all those tasks - and a little more. 这是一个完成所有这些任务的downloadfiles()版本-还有更多。

public function downloadfiles()
{
    // get the array of the URI segments after the controller/method parts
    $segs = array_slice($this->uri->segments, 2);
    // turn that array into an "absolute path" string 
    $fullpath = FCPATH . implode("/", $segs);

    // add a check to see if that path/file exists
    if( ! file_exists($fullpath)){
        echo "File $fullpath not found";
        return;
    }

    force_download($fullpath, NULL);
}

Let me know how this works. 让我知道这是如何工作的。

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

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