HP371 HP371 - 1 month ago 10
PHP Question

readfile return open stream: HTTP request failed! HTTP/1.1 403 Forbidden in php

I have create one function in Codeigniter controller for download file, here is code for that.

function download_example($file_name) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);
ob_clean();
flush();
readfile(base_url().'download_example/'.$file_name);
}


But after download in file show 403 Forbidden error like this.




A PHP Error was encountered

Severity: Warning Message:
readfile(http://example.com/download_example/example.txt): failed to
open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Filename: controllers/controllers.php Line Number: 371



Answer

Change to something like:

readfile(dirname(__FILE__).'/download_example/'.$file_name);

If it is local file and current PHP script's directory is same as download_example directory. Change to match your system. Also make sure that file does exists and that user where web server is running has permission to access this file.

Comments