Reputation: 529
I want block the direct access to some of public files from my directory. For example, I have the file image.png and I have to fill a captcha to download that file, if I fail the captcha I don't want that the user could access the file. Is there any way to do that in Symfony?
Upvotes: 0
Views: 438
Reputation: 1587
First of all, create an .htaccess
file inside the image directory to prevent the direct access of files inside the directory.
Deny from All
Now, give customised path, through controller
to download the file, where you can easily integrate a captcha
by using some bundle like Gregwar's CaptchaBundle.
On successful captcha validation, download the file through Response
.
$filename = __DIR__ . "../path_to_file/image.png";
// Generate response
$response = new Response();
// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '";');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(file_get_contents($filename));
Note : Code not tested!
Hope this helps!
Upvotes: 2