upload() form()

A multi-file upload widget (blueimp jQuery File Upload) from the form2/upload template.

form()->upload($name = '', $desc = '', $extra = [], $replace = [])

Options

OptionMeaning
$nameDefault image.
descLabel.
(template options)The options go to the template as option, the values as item.

Example

Select file...

Code

return form()->upload('photos', 'Photos');

Notes

  • The upload endpoint is your module action: check type and size, and store the files under generated names.
  • A ready endpoint: _class('form2_file_handler', 'classes/form2/')->process() in that action. It takes only a POST from a logged-in user (an admin in the admin section; ACCESS admin: admins only) with the session CSRF token (_token field or X-CSRF-Token header), stores images and common documents (ALLOWED_EXTENSIONS, content checked with fileinfo) under random names in UPLOAD_DIR (default uploads/form2_files/), sends CORS headers only to CORS_ORIGINS (empty: same origin), and answers 503 "not available" when the jQuery-File-Upload library is not installed (it is never cloned at run time). No file list, delete or chunked uploads.
  • Files of the handler: at most MAX_FILE_SIZE bytes (default 10 MB, 0: the PHP limits alone); any file with <?php in its first 64 KB is refused, whatever its type, and a text file (txt, csv, rtf and any allowed type not in BINARY_EXTENSIONS: images, pdf, office) also with <?=; jpg, png, gif and webp are re-encoded with GD before they are stored (REENCODE_IMAGES, on by default when GD reads the type), which keeps the pixels and drops anything appended or embedded (EXIF and gif animation too), and an image GD cannot read is refused; stored files get mode 0644. Office files must be named by fileinfo as their own type: OFFICE_GENERIC_TYPES = true also takes application/zip and application/octet-stream for them (an old libmagic). ACCESS admin refuses every user-side request (403), whoever is logged in. A folder that cannot be made or protected answers 500.
  • The same checks on a file that did not come through the widget (a plain $_FILES upload of a plugin, a file a job fetched): check_file($path, $original_name, ['dest' => ..., 'uploaded' => true]) answers ['ok', 'error', 'ext', 'path', 'size'] and re-encodes an image in place or into dest (docs misc/upload_checks).
  • The .htaccess the handler writes into its folder works on Apache only. With nginx or Caddy, deny PHP under uploads/ in the server config, as the nginx and Caddy configs of the install page do. nginx, before the PHP location: location ~* ^/uploads/.*\.(php|phtml|phar|pht) { deny all; }. Caddy, before php_fastcgi: @uploads_php path_regexp (?i)^/+uploads/.*\.(php|phtml|phar|pht) and error @uploads_php 403.

See also: form/file