upload() form()
A multi-file upload widget (blueimp jQuery File Upload) from the form2/upload template.
form()->upload($name = '', $desc = '', $extra = [], $replace = [])Options
| Option | Meaning |
|---|---|
$name | Default image. |
desc | Label. |
(template options) | The options go to the template as option, the values as item. |
Example
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;ACCESSadmin: admins only) with the session CSRF token (_tokenfield orX-CSRF-Tokenheader), stores images and common documents (ALLOWED_EXTENSIONS, content checked with fileinfo) under random names inUPLOAD_DIR(defaultuploads/form2_files/), sends CORS headers only toCORS_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_SIZEbytes (default 10 MB,0: the PHP limits alone); any file with<?phpin its first 64 KB is refused, whatever its type, and a text file (txt, csv, rtf and any allowed type not inBINARY_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 takesapplication/zipandapplication/octet-streamfor them (an old libmagic).ACCESSadminrefuses 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
$_FILESupload 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 intodest(docsmisc/upload_checks). - The
.htaccessthe handler writes into its folder works on Apache only. With nginx or Caddy, deny PHP underuploads/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, beforephp_fastcgi:@uploads_php path_regexp (?i)^/+uploads/.*\.(php|phtml|phar|pht)anderror @uploads_php 403.
See also: form/file