Please tell us how to properly organize the download files from your site. The MVC scheme is used, and when I click on the direct download link http://site.ru/template/load/Catalog.pdf, he turns to the controller from me and gives an error and does not download anything, the error is as follows:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'SiteController' does not have a method 'actionIndextsite' in /.../site.ru/components/Router.php on line 39 

How to be, how to write an action to download a file?

    2 answers 2

    You have an error in the application code.

    In Router.php on line 39 there is a call to the actionIndextsite method in the SiteController class, but there is no such method in the class. Perhaps, an error in writing the method (check, maybe it is called actionIndexTSite, which is more consistent with the style camelCase).

    • there is no mistake, he says that there is no action method, more precisely the path in the routes as I understand it. I need to learn how to organize downloading files through the controller or I don’t know how it all is ... - Rew
    • If you need to download via the controller, do it in the controller as follows: 1. Read the file name from request or, better, its id (save the path to the file in the database). 2. Read the file from the file system (save to variable). 3. Send the Content-type header corresponding to the file type. 3. Output the contents of the variable into which the file was read. All this requires a certain accuracy. You need to think about security so that no one can load the conditional / etc / password. A large file size can kill a process if the maximum size specified in php.ini in memory_limit is exceeded. - AlexDmitriev

    Fully working code ...

     $filename= ROOT . "/load/catalog.pdf"; $file_name="catalog.pdf"; header("Cache-control: private"); header("Content-type: application/force-download"); header("Content-Length: ".filesize($filename)); header("Content-Disposition: filename=".$file_name); readfile($filename);