I have hit a serious problem with a project I've been working on for a while now.
A user uploads files to a course. The upload script takes the filename, replaces spaces with an underscore, removes the file extension, adds a 4 digit number and then adds the extension back on.
However, the files are uploaded perfectly to the server (when opened directly from the "uploads" folder, they are fine). When downloaded to the user's computer, the files all become corrupted except for DOCX files and images.
PDF, Excel (xlsx) and powerpoint (pptx) give an error message when the downloaded file is opened: "The file is corrupt and cannot be opened".
The code I have to download is:
<?php
class download extends Controller {
public function resource ($filename)
{
$filepath = realpath('./uploads/resources/courses/' . $filename);
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: no-cache');
readfile($filepath);
}
}
It is an MVC, so that's why it's in a class, $filename is retrieved from a $_GET variable of the file name.
Any help would be much appreciated!