0
votes

I have followed Cakes Cookbook to send files (http://book.cakephp.org/3.0/en/controllers/request-response.html#sending-files) but I've been facing a weird problem. PDF's, DOC's and other binaries work just fine. But when I try to download/show an image (JPG or PNG) the file corrupts itself.

The downloaded file is not recognized as an image. It has the exactly same size of the original one, but when I diff it they are completly different.

I couldn't find anything like at the internet related to cake so I hope you can help me!

The below code is my download action

 public function arquivo($id) {
    $file = $this->Arquivos->get( $id );
    
    $this->response->file($file['filename'], ['download' => true]);
    // Return response object to prevent controller from trying to render
    // a view.
    
    return $this->response;
}

Response headers:

Accept-Ranges:bytes

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Connection:Keep-Alive

Content-Length:121000

Content-Type:image/jpeg

Date:Thu, 24 Nov 2016 16:17:49 GMT

Expires:Thu, 19 Nov 1981 08:52:00 GMT

Keep-Alive:timeout=5, max=100

Pragma:no-cache

Server:Apache/2.4.10 (Ubuntu)

3
Did you checked $file['filename'] is the full path of your file? - Sumon Sarker
Yes I logged it and it is the correct one. Also, it does send a file with the same size and name. And, as I said, pdf files do work - Fernando Rybka
You should debug the file MIME type for images. - Sumon Sarker
Thanks @SumonSarker ! I added Response Headers in my question. Do them show something unusual? (sorry, but I'm not familiarized with those parameters) - Fernando Rybka
File MIME/content type is very important for knowing correct file type. If this type is somehow changed or not proper, Then file will corrupted. - Sumon Sarker

3 Answers

0
votes

Check your file path and MIME Types is correct.

public function arquivo($id) {
    $file = $this->Arquivos->get( $id );
    $this->response->file(
        $file['filename'], #Check $file['filename'] is full path of your download file
        [
           'download' => true,
           'name' => 'Your_Download_File_Name_Here'
        ]
    );
    return $this->response;
}

Example:

Your $file['filename'] variable should be /path/to/your/file.jpg

Also check the correct MIME TYPES from CakePHP 3 Sending or Downloading Files

0
votes

I've got the same problem with downloading jpg and xlsx files.

My code is quite simple:

public function download() {
    $response = $this->response->withFile($template_file_path, ['download' => true]);
    return $response;
}

Compared with 2 files (original file AND the downloaded file which is corrupted) with a binary editor, there is 1 byte difference. I don't know why but the 0x20 was added at the top of the file.

0
votes

I've got the same problem and I was able to solve it. The problem was caused as soon as a recent change on my source code for another controller was made so I started searching for spaces after or before the opening php tag<?php <?. Also I deleted the closures ?> for all of my php files. I know it is hard to detect where the problem comes from but make sure to check the last files you changed. i.e controllers, behaviors, models etc.

I hope it can solve your problem.