2

Using Cake version 2.4.3 stable

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/

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

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.

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

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?

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.

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

View code :

    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;

}

And the upload / save

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));
}

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.

0

2 Answers 2

4

The problem:

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.

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.

The Solution:

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).

Path to use in Controller:

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)
1
  • Excellent. This is the missing piece I have been looking for.
    – hyperweb
    Dec 21, 2013 at 21:42
0

Create a symbolic link to your folder inside of the webroot ;-)

1
  • 1
    This defeats the entire purpose of putting files outside the webroot in the first place.
    – Dave
    Dec 21, 2013 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.