Skip to main content
Version: Fegg 5.x

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"].

Controller
<?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:

  1. Upload errors and the size (@empty, @size, @failed).
  2. The extension must be allowed by setFileType. Script extensions (php, phtml, phar, cgi ...) are refused even when listed (@type).
  3. The content must match the extension (@content): images (jpg, jpeg, png, gif, webp) must be readable by getimagesize(), and for the extensions of Upload::MIME_TYPES the MIME type reported by finfo must 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 0755 and files saved with 0644.

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 finfo when 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:

Controller
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​