CakePHP-在webroot外部保存和提供文件 [英] CakePHP - Saving and serving files outside of webroot

查看:71
本文介绍了CakePHP-在webroot外部保存和提供文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Cake 2.4.3稳定版

Using Cake version 2.4.3 stable

所以我在这里有空,我修改了此插件以将文件保存到应用程序文件夹https://github.com/srs81/CakePHP-AjaxMultiUpload/

So I am having a time here, I have modified this plug in to save files to the app folder https://github.com/srs81/CakePHP-AjaxMultiUpload/

已将上传的文件保存到/public_html/app/files

The uploaded files are being saved to /public_html/app/files

现在奇怪的是,当尝试将这些文件提供给视图时,我可以读取文件名,文件大小,但不能读取文件本身,图像,pdf等.

Now the strange thing is, when trying to serve these files to the view, I can read the file name, the file size, but not the file itself, image, pdf, etc.

当我尝试通过直接URL访问文件时,出现此错误:

When I try to access the file via direct url, i get this error :

错误:找不到FilesController.

Error: FilesController could not be found.

我怎么能读取文件名,文件大小而不是文件本身?

How can it be that I can read the file name, the file size, but not the file itself?

需要将文件保存在app文件夹中.这些文件应受保护,因此,只有登录到该应用程序的用户才能访问这些文件.我尝试使用cake send文件响应,但没有结果.

There is a need to save the files in the app folder. These files should be protected and therefore only be accessed by users who are logged into the application. I have tried to use cake send file response with no outcome.

我想这里真正的问题是如何从APP读取文件.DS.文件"?

Edit : I guess the real question at hand here is how can I read files from APP . DS . 'files' ?

查看代码:

    public function view ($model, $id, $edit=false) {
    $results = $this->listing ($model, $id);

    $directory = $results['directory'];
    $baseUrl = $results['baseUrl'];
    $files = $results['files'];

    $str = "<dt>" . __("Pictures") . "</dt>\n<dd>";
    $count = 0;
    $webroot = Router::url("/") . "ajax_multi_upload";
    foreach ($files as $file) {
        $type = pathinfo($file, PATHINFO_EXTENSION);
        $filesize = $this->format_bytes (filesize ($file));
        $f = basename($file);
        $url = $baseUrl . "/$f";
        if ($edit) {
            $baseEncFile = base64_encode ($file);
            $delUrl = "$webroot/uploads/delete/$baseEncFile/";          
            $str .= "<a href='$delUrl'><img src='" . Router::url("/") . 
                "ajax_multi_upload/img/delete.png' alt='Delete' /></a> ";
        }
        $str .= "<img src='$url' /> ";
        $str .= "<a href='$url'>" . $f . "</a> ($filesize)";
        $str .= "<br />\n";
    }
    $str .= "</dd>\n"; 
    return $str;

}

上传/保存

public function upload($dir=null) {
    // max file size in bytes
    $size = Configure::read ('AMU.filesizeMB');
    if (strlen($size) < 1) $size = 20;
    $relPath = Configure::read ('AMU.directory');
    if (strlen($relPath) < 1) $relPath = "files";

    $sizeLimit = $size * 1024 * 1024;
            $this->layout = "ajax";
        Configure::write('debug', 0);
    $directory = APP . DS . $relPath;

    if ($dir === null) {
        $this->set("result", "{\"error\":\"Upload controller was passed a null value.\"}");
        return;
    }
    // Replace underscores delimiter with slash
    $dir = str_replace ("___", "/", $dir);
    $dir = $directory . DS . "$dir/";
    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
    }
    $uploader = new qqFileUploader($this->allowedExtensions, 
        $sizeLimit);
    $result = $uploader->handleUpload($dir);
    $this->set("result", htmlspecialchars(json_encode($result), ENT_NOQUOTES));
}

从原始插件进行的唯一真正更改是将WWWROOT更改为APP以进行切换.保存效果很好,很可爱.但这很奇怪,我可以在视图中读取文件名,文件大小,但不能查看图像,pdf等.

The only real change from the original plug in is changing WWWROOT to APP to make the switch. Saving works fine and lovely. But its just weird that I can read the file name, file size in the view no problem but cannot view an image, pdf, etc.

推荐答案

问题:

您不能通过直接URL显示在Webroot之外的文件-这就是将它们放在Webroot上方的主要目的-使其无法直接访问.

You cannot display files that are outside the webroot via a direct URL - that's the whole point in putting them above the webroot in the first place - to make them NOT directly accessible.

因此,首先在控制器中构建图像src路径(无论如何都不是一种好习惯)不会改变以下事实:当该路径到达浏览器时,它将读取图像源,就像试图从上方拉出图像一样webroot,它不会/不能.

So building the image src path in the controller first (not good practice anyway) won't change the fact that when that path gets to the browser, and it reads the source of the image as trying to pull an image from above webroot, it won't/can't.

解决方案:

解决方案是使用 CakePHP的发送文件(以前是媒体视图).然后,将img的"src"设置为控制器/动作.在该操作中,它基本上说将该操作显示为图像"(或pdf或其他格式).

The solution is to use a CakePHP's Sending files (what used to be Media Views). Then, you set your img's "src" to a controller/action. In that action, it basically says "display this action as an image" (or pdf, or whatever else).

在Controller中使用的路径:

从我的一个应用程序中完全按照上面的方法进行操作,这是我在文件"(与webroot级别相同)中用于文件的路径:

took this from one of my apps which is doing exactly the above, and here's the path I used for a file in 'files' (same level as webroot):

file_exists(APP . 'files' . DS . $your_filename)

这篇关于CakePHP-在webroot外部保存和提供文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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