Introduction
Uploads and file checks
How to use
Import Fegg\Tool\Upload and create it with new. Set the upload directory, then call Upload::upload with the name of the input[type="file"].
<?php
namespace Site\Controller;
defined('FEGG_CORE_DIR') || exit;
use Fegg\Tool\Upload;
class Entry extends Controller
{
public function post()
{
$this->loadConfig('upload_dir');
$upload = new Upload();
$upload->setUploadDir($this->config['upload_dir']['path'] . '/entry');
$upload->setMaxFileSize(5);
$upload->setFileType(array('jpg', 'jpeg', 'png', 'pdf'));
if ($upload->upload('file', uniqid('f_'), 'Attachment')) {
$data = $upload->getUploadedData('file');
// $data['name'], $data['org_name'], $data['type'], $data['size'] ...
} else {
$errors = $upload->getErrorMessage('file');
}
}
}
$this->loadConfig('upload_dir') reads site/config/upload_dir.php (path htdocs/_fegg/site/up_dir and url /_fegg/site/up_dir) into $this->config['upload_dir']. The CMS screens (Fegg\Controller\Cms\Image, Media, Multi) use this class in the same way.
Error messages are defined in file_error_message.php, see Configuration.
What is checked
Upload::upload checks, in order:
- Upload errors and the size (
@empty,@size,@failed). - The extension must be allowed by setFileType. Script extensions (
php,phtml,phar,cgi...) are refused even when listed (@type). - The content must match the extension (
@content): images (jpg,jpeg,png,gif,webp) must be readable bygetimagesize(), and for the extensions of Upload::MIME_TYPES the MIME type reported byfinfomust be one of the types listed for the extension. A PHP file renamed to.jpg, or a text file named.pdf, is refused.
When the file is saved:
- Files are saved under the name you give (
uniqid('f_'), ...), never under the visitor's file name. Keep the original name (org_name) in the database only. - Directories are created with mode
0755and files saved with0644.
MIME types
Upload::MIME_TYPES is the one table of types by extension (jpg, png, gif, webp, pdf, the Office formats such as docx, xlsm, pptx, and zip, txt, csv). For each extension:
- every type listed is accepted from
finfowhen the file is uploaded; - the first type is the one the stored file is sent with (Upload::mimeType).
Upload::MIME_TYPES['docx'];
// array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip', 'application/octet-stream')
An extension that is not in the table is checked by its extension only, and sent with the type finfo reads from it.
Protecting the upload directory
The upload directory htdocs/_fegg/site/up_dir/ is inside _fegg/, which is denied; the root .htaccess lets /_fegg/site/up_dir/ through and sends a file that is not there (an image size not made yet, /file/<id>/) to PHP. When the upload directory is below the document root, setUploadDir writes an .htaccess into it (Upload::HTACCESS, an existing file is left alone) that serves only the file types that can be uploaded and runs no script, so an uploaded file can never run as code. The CMS dashboard also writes it when it opens, and the skeleton ships the file.
fegg-cli doctor and the CMS dashboard report a missing site/up_dir/.htaccess or a missing root rule (see fegg-cli). Remember it when you upload over FTP: many FTP clients skip dotfiles. On nginx, .htaccess is ignored: use the location ^~ /_fegg/site/up_dir/ block of Directory structure.
Sending stored files
The CMS sends stored files (/_fegg/site/up_dir/file/<id>/, the media and image screens) with Fegg\Http\Download::sendHeaders(). It sends X-Content-Type-Options: nosniff, a valid Cache-Control, and an RFC 6266 Content-Disposition with the original name escaped (an ASCII fallback plus the UTF-8 name). HTML, SVG, XML and JavaScript files are always sent as attachments, never shown inline. Use it when your own controller sends a stored file:
use Fegg\Http\Download;
Download::sendHeaders(array(
'type' => $file['type'],
'size' => filesize($path),
'org_name' => $file['org_name'],
), false); // false: browser cache only (private); true: public
$this->halt(file_get_contents($path));
Methods
Upload::__construct
Create the uploader and load the error messages
Upload::setUploadDir
Set the upload directory
Upload::getUploadDir
Get the upload directory
Upload::setMaxFileSize
Set the maximum file size (MB)
Upload::getMaxFileSize
Get the maximum file size (MB)
Upload::setFileType
Set the allowed file types (extensions)
Upload::getFileType
Get the allowed file types (extensions)
Upload::setUploadInfo
Set the upload options at once
Upload::upload
Check an uploaded file and move it to the upload directory
Upload::getUploadedData
Get the information of the uploaded file
Upload::isError
Check whether any upload error occurred
Upload::getErrorMessage
Get the error messages of a file
Upload::mimeType
Get the MIME type a stored file is sent with