phpcodeigniterfile-io

404 File Not Found Error while trying to download an EXISTING file


I am trying to make a form using CI. Some text data is inserted into the DB and a file is stored into a folder like /upload/books/12/some_book.zip.

upload folder is located next to application folder of CodeIgniter. There are a few more folders containing accessible files inside.

c:\laragon\www\my_ci_project

My folder hierarchy is as follows:

my_ci_project
├─application
├─system
├─inc
├─includes
├─upload
  └─books
    ├─10
    ├─11
    └─12
      └─some_book.zip 

My test url on local server is http://my_ci_project.test/

Everything works fine. I can see the file that resides in that folder using FTP client. But when I try to download the file clicking its link lie

<a href="http://my_ci_project.test/upload/books/12/some_book.zip">download here</a>

the browser returns 404 error.

Strange but

If I type

http://my_ci_project.test/upload/books/12/some_book.zip

in the browser's address bar manually, it downloads the file.

It also does not download file on my local server.

The file's CHMOD is 644 and the folders' are 755.

I have tried to use CI's download helper.

force_download($path_to_file);

I get totally blank page.

I've tried to understand that the file is really where it is called.

$file_exists = file_exists(base_url().'upload/books/12/some_book.zip');

returns FALSE.


Solution

  • After a short search I've noticed that I should tell the browser what kind of file it is.

    public function download($file_path)
    {
        //$file_path = base_url() . 'upload/books/12/some_book.zip';
        $file_contents = file_get_contents( $file_path );
    
        force_download($file_path, $file_contents);   
    }
    

    It is working now.