使用php下载文件之前检查文件 [英] check the file before downloading the file using php

查看:72
本文介绍了使用php下载文件之前检查文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下载文件之前检查文件,
i具有控制器下载功能并具有download_file

i want to check the file before downloading the file, i have controller Download and function download_file

这是我的代码:
i太累了代码,但我总是不知道

here is my code : i tired this code and i always get no

public function download_file($namafile,$nofile)
        {
    $filecek =(site_url()."/uploads/file/".$namafile);
        $data = file_get_contents(site_url().'/uploads/file/'.$namafile);

    echo "$filecek";

    if (!file_exists($filecek)) {
      echo "no";
    }else {
force_download($namafile,$data);
      echo "yes";
    }

        }
    }

是下载链接

<a href="<?php echo base_url(); ?>download/<?php echo $getFiles['file']; ?>/<?php echo $getFiles['nofile']; ?>"><i class="icon-download"></i> <?php echo $getFiles['kategori']; ?></a>


推荐答案

site_url()不会那样工作,因为您总是会获得网站网址...

site_url() won't work that way because you always get the Website Url ...

file_exists()但是会检查服务器上是否存在目录或文件-因此您需要一个绝对位置。

file_exists() however checks whether a directory or file exists on your server or not - so you need an absolute position..

有一个 CONSTANT 称为 FCPATH ,您可以使用它-指向 index.php 所在的目录

There is a CONSTANT called FCPATH which you can use - it points to the directory where your index.php resides.

所以基本上可以尝试以下

So basically you can try the following

public function download_file($namafile,$nofile)
{
    $filecek = FCPATH."uploads/file/".$namafile;

    echo "$filecek";

    if (!file_exists($filecek)) 
    {
        echo "no";
    }
    else 
    {
        force_download($namafile, NULL);
        echo "yes";
    }
}

唯一的条件是- uploads / file / 存在于您的 FCPATH 中。

the only condition here is - that uploads/file/ exists within your FCPATH.

这篇关于使用php下载文件之前检查文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆