emersonthis
emersonthis

Reputation: 33408

CakePHP: how to allow public access to files in a directory besides /webroot

I want to be able to open pdfs that live in a folder at /app/somefile/file.pdf via apache like this http://mysite/app/somefile.file.pdf. I've tried adding a RewriteCond in CakePHP's .htaccess file:

RewriteCond %{REQUEST_URI} !^/app/somefolder/ - [L]

But just get a 500 error. What am I doing wrong?

Upvotes: 2

Views: 3955

Answers (4)

Alex Stallen
Alex Stallen

Reputation: 2252

Use this in your controller and use routes to access it the way you want, opening up other folders for the world is NOT a good idea

Sending files

There are times when you want to send files as responses for your requests. Prior to version 2.3 you could use Media Views to accomplish that. As of 2.3 MediaView is deprecated and you can use CakeResponse::file() to send a file as response:

public function sendFile($id) {
    $file = $this->Attachment->getFile($id);
    $this->response->file($file['path']);
    //Return reponse object to prevent controller from trying to render a view
    return $this->response;
`enter code here`}

As shown in above example as expected you have to pass the file path to the method. CakePHP will send proper content type header if it’s a known file type listed in CakeReponse::$_mimeTypes. You can add new types prior to calling CakeResponse::file() by using the CakeResponse::type() method.

If you want you can also force a file to be downloaded instead of being displayed in the browser by specifying the options:

$this->response->file($file['path'], array('download' => true, 'name' => 'foo'));

source: http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file

Upvotes: 1

emersonthis
emersonthis

Reputation: 33408

I was able to do this buy only adding this to the .htaccess file in the root:

RewriteCond %{REQUEST_URI} !^/app/somefolder/

(My original version had a [L] which is incorrect, and that's why it wasn't working.)

Just in case anyone doesn't already know: this is not generally a secure thing to do. I have very specific reasons for doing this.

Upvotes: 0

Jim Naumann
Jim Naumann

Reputation: 187

you could just make a symlink to them, though your apache config may or may not be allowed to follow them.

Upvotes: 0

kaisk23
kaisk23

Reputation: 419

You could use an Apache alias to make the contents of that directory publicly accessible:

Alias /app/somefile /app/webroot/somefile

Place the above code in a server/virtual host config file, not .htaccess.

Make sure the web server user has read access to that directory.

Upvotes: 0

Related Questions