file download --- heelppp

Permalink
Hi, running 8.5.2 i'm currently building a function to download files (pdf, txt, etx) stored by other application in a server directory (lets say in root: "foldername/test.pdf", renaming them in a new way. So they are files that DO NOT exist in standard file manager from C5.

A) using standard methods of download does not work:

header('Content-type: application/pdf');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-disposition: attachment; filename=\"" . basename($myfile) . "\"");
header('Expires: 0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Pragma: public');
header("Content-Length: " . filesize($myfile));
header("Content-type: ".filetype($myfile));
header("Content-Transfer-Encoding: Binary");
ob_clean();
flush();
readfile($myfile);

B) in C5 documentation (https://documentation.concrete5.org/developers/working-with-files-an... i've found this instruction:

$service = \Core::make('helper/file');
$service->forceDownload('/path/to/my/file.zip');

but does not work !!

whatever file i do submit to download (pdf, txt, etc) what I get is alway a file with a text content as the attached one ?!?!?

--- >>> Can anybody help me ? I'm in a critical development and do not know what to do more :-(

Thanks
Michele from Rome (and sorry for my bad english)

1 Attachment

 
RikZuiderlicht replied on at Permalink Reply
Hello,

I have tested this from a package controller and was enable to download the file from the private folder.

The solution is from an answer on stackoverflow.
https://stackoverflow.com/questions/8911431/download-file-from-non-p...

An example of the implementation in concrete5:

public function downloadFile($fileName)
    {
        $file = DIR_BASE . '/../private_folder/' . $fileName;
        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);