4.0 is a security release. The API is mostly unchanged, but new protections are on by default and will refuse some uploads that 3.x accepted. The changelog has the full list.
Coming from codeguy/upload? Replace the \Upload namespace with \GravityPdf\Upload
to get the 3.x API, then continue here.
composer require gravitypdf/upload:^4.0PHP 7.3 through 8.5, still requiring only ext-fileinfo. ext-mbstring is now suggested:
without it, sanitized filenames aren't guaranteed to be valid UTF-8. See
Requirements.
With no validations configured, upload() throws \LogicException. That's a different
type from the \GravityPdf\Upload\Exception a rejected file throws, so a misconfigured
object can't end up in the branch where you handle a bad upload.
If you catch \GravityPdf\Upload\Exception around upload(), add \LogicException.
A catch (\Exception $e) needs no change.
use GravityPdf\Upload\Validation\FileType;
$file->addValidation(new FileType('pdf', 'application/pdf'));
// Or keep the 3.x behaviour of storing whatever arrives
$file->allowUnvalidatedUploads();Storage\FileSystem won't write a file whose extension is in
FileSystem::getDefaultBlockedExtensions(): things a server executes or reads as config
(php, cgi, exe, htaccess, config) and markup a browser renders (html, svg,
js, xml). Full table in the README.
The refusal throws \GravityPdf\Upload\Exception from upload(). It isn't a validation
error, so it doesn't appear in getErrors().
Only the last dot-separated component is an extension. FileInfo::setName() rewrites
interior dots to hyphens as it did in 3.x, so release.config.zip stores as
release-config.zip and is written. release.config and settings.ini are refused. A file
named just php, with no extension, is still stored. (upload() checks every component
regardless, since a custom FileInfoInterface can hand it a name setName() never saw.)
To keep a blocked extension:
use GravityPdf\Upload\Storage\FileSystem;
// Drop the entries you need, keeping the rest of the list
$storage->blockExtensions(
array_diff(FileSystem::getDefaultBlockedExtensions(), ['config'])
);
// Accepting SVG means dropping two entries, not the whole markup group.
// Sanitize the contents yourself
$storage->blockExtensions(
array_diff(FileSystem::getDefaultBlockedExtensions(), ['svg', 'svgz'])
);
// Or restore the 3.x behaviour of writing any extension
$storage->allowAnyExtension();Two things to check if you pass your own list:
- Entries are split on dots.
'tar.gz'never matched anything in 3.x; it now blockstarandgzseparately, so a dotted entry covers more than it names. blockExtensions()requires a non-empty array and throws on[], so a missing config value can't quietly disable the check.allowAnyExtension()is the only way to empty the list.
3.x left permissions to the umask, usually producing a world-readable 0644. If another
account serves or processes the uploads, give it access:
$storage->setMode(0644); // or null to leave it to the umask, as 3.x didFile::humanReadableToBytes(), used by Validation\Size, now understands a trailing B.
3.x read only the last character, so '5MB' was 5 bytes.
Check every size bound using a KB/MB/GB suffix, since each becomes much larger.
Bounds without the trailing B are unchanged.
Check any bound using a unit other than B, K, M or G as well. 3.x read the unit
as bytes, so new Size('1T') was a one-byte limit that rejected everything. It now throws.
Unparseable input like 'abc' throws InvalidArgumentException instead of becoming 0,
a negative size like '-5M' throws, and fractions like '0.5M' work.
$hash = $fileInfo->getMd5(); // 3.x
$hash = $fileInfo->getHash('md5'); // 4.0: the same digest
$hash = $fileInfo->getHash(); // 4.0: SHA-256Removed from FileInfo and FileInfoInterface. If you compare uploads against digests
stored by 3.x, pass 'md5' or migrate the stored values.
When nothing was selected, or the file exceeded PHP's own limits, 3.x still built a
FileInfo for a single-file field and reading its metadata raised an uncaught error. 4.0
leaves it out, matching what multi-file fields always did: count($file) is 0, forwarded
metadata calls return null, and the message is already in getErrors().
if (count($file) === 0 || $file->isValid() === false) {
// getErrors() explains what went wrong
return;
}upload() throws on an empty collection rather than returning true having stored
nothing, so check the count if an empty field is a valid outcome for your form.
A multi-file field where only some of the files failed behaves as it did in 3.x:
upload() stores all of them or none. To keep the files that passed, call uploadValid()
where you call upload() and check what it returns instead of catching a validation
failure.
if ($file->uploadValid() === false) {
// getErrors() names the files that were rejected. The rest are already stored, so
// if you abandon the request here, undo getUploadedLocators() yourself
}Nothing throws for a rejected file, so that return value is the only signal one was. A single-file field gains nothing from the swap, since it's stored whole or not at all.
setExtension() lowercases and trims, then discards the extension if anything other than
letters and digits remains. 3.x deleted the offending characters, which could build an
extension the client never sent. Files that used to land with a repaired extension now land
with none; add a FileType validation to reject them up front.
Validation\Extension and Validation\Mimetype still work and raise no notice, but they're
deprecated. Side by side they check two independent allow-lists, so a file can pass both
while the two answers describe different formats. FileType requires the extension and the
sniffed contents to agree:
// 3.x
$file->addValidations([
new Extension(['png', 'gif']),
new Mimetype(['image/png', 'image/gif']),
]);
// 4.0
$file->addValidation(
(new FileType('png', 'image/png'))->allow('gif', 'image/gif')
);One format per allow() call, or every extension is paired with every media type.
FileInfoInterface::getHash(string $algorithm = 'sha256'): string is now declared, and
getMd5() is not.
setName(), setExtension() and setNameWithExtension() no longer declare a return
type. They declared : FileInfo, so an implementation that didn't extend FileInfo
couldn't satisfy them: omitting the type was a fatal, and matching it compiled and then
raised a TypeError on the first call. Delete : FileInfo from your own setters and
return $this. Leaving it keeps the same TypeError, since the declaration is your
class's. FileInfo keeps it on its own methods, so existing subclasses need no change.
StorageInterface::upload() now specifies its return value: a locator your storage
defines, passed back unchanged by File::getUploadedLocators(). Only FileSystem promises
an absolute local path, so the unlink() rollback in the README suits it and not
necessarily yours. Never return ''; throw instead.
Throw \GravityPdf\Upload\Exception from custom validators. A \LogicException
propagates out of isValid() untouched, since it signals broken code rather than a rejected
file. Anything else is still caught, but neither its message (which can contain server
paths) nor its class name reaches getErrors(). Catch it in the validator and rethrow an
Upload\Exception if either belongs in what the user sees. FileInfo::getHash() throws
\InvalidArgumentException for an unsupported algorithm for the same reason: a misspelled
algorithm is broken code, not a rejected file.
Storage\FileSystem no longer trusts the name a FileInfoInterface returns. It reduces
the name to its basename() and rewrites the characters Windows disallows (< > : "
| ? *) to -. It refuses a name that starts with a dot, contains control or bidi
characters, resolves to a Windows device such as CON.txt, or points at a symlinked
destination. A name can no longer select a subdirectory; construct the FileSystem with
it instead. The shipped FileInfo already rewrites or blanks all of these.
If you subclass File. isValid(), upload() and uploadValid() are final, so an
override of any of them is a fatal error when your class is loaded. $errors and
$constructorErrors are gone, replaced by a private $errorDetails holding each error as its
parts. Reading, writing or isset()-ing either name throws \LogicException, and a subclass
that declares one of them — or $errorCodeMessages — is refused when it is constructed, since
a declared property is in scope and records into itself where nothing reads it. Record through recordError(),
still protected, which now takes the message and the filename separately:
recordError(string $messageId, array $args = [], string $errorCode = ErrorCode::NONE, ?string $filename = null). A one-argument call is unchanged. Read with getErrors().
What is still a seam, and what is not.
| If you overrode… | Now do… |
|---|---|
File::isValid(), File::upload(), File::uploadValid() |
Implement ValidationInterface, which all three run. It is handed one file at a time, so a check across the whole batch runs before you call upload() |
$this->errors[], $this->constructorErrors |
recordError() to write, getErrors()/getErrorDetails() to read |
File::$errorCodeMessages |
Override getUploadErrorMessages() |
FileInfo::isUploadedFile() |
Still a seam — pair it with FileSystem::acceptFilesNotUploadedByPhp() |
FileInfo::getReservedWindowsNames() |
Still a seam |
FileInfo::sanitizeName() |
Still a seam for setName(), but setExtension() re-fits the name afterwards through private code an override does not see |
FileSystem::resolveFilename() |
Still a seam, but no longer controls refusals |
FileSystem::moveUploadedFile() |
Still there, still protected |
Validation\Size::scale() is new, and the seam for the one question the wording leaves open:
it names the unit and formats the number, decimal separator included.
File::init() and the four magic property methods — __get(), __set(), __isset(),
__unset() — are new in 4.0.0 and final. init() is the tail both constructors share, so
constructor work of your own goes in your own __construct(); the four are the only route to
the guard that refuses the replaced names above, so declare a property of your own rather than
making it magic.
Other changes:
-
blockExtensions()called with no argument no longer means the default list: passFileSystem::getDefaultBlockedExtensions(). -
File::__call()throws\BadMethodCallExceptionandFileInfo::createFromFactory()throws\LogicException, both previously\GravityPdf\Upload\Exception. -
Exception::__construct()declaresstring $messageand takes the error code and the message's values before$codeand$previous:__construct(string $message, ?FileInfoInterface $fileInfo = null, string $errorCode = ErrorCode::NONE, array $messageArgs = [], int $code = 0, ?Throwable $previous = null).new Exception($message, $fileInfo)is unaffected, which covers nearly every throw. But a validator passing$codeor$previouspositionally is now passing them to the wrong parameters. Subclasses need no change: PHP exempts constructors from signature compatibility. -
Six error messages were reworded. All are strings
getErrors()returns:3.x 4.0.0 Must be less than or equal to: 5242880Must be no more than 5 MBMust be greater than or equal to: %1$sMust be at least %1$s MBMissing a temporary folderThe server is missing its temporary upload folderFailed to write file to diskThe server could not write the file to diskIs not an uploaded fileThis file was not received as an uploadUnknown ErrorUnknown errorValidation\Sizenow states the bound in the largest of B/KB/MB/GB it reaches, so it has eight message ids where it had two, andgetErrorDetails()['args']holds the scaled amount rather than a byte count. Anything matching these strings needs updating; matchErrorCodeconstants instead. -
Both validation callbacks now fire for every file, including one that fails the uploaded-file check.
afterValidatewas previously skipped for those. -
The filename rules moved to the new
GravityPdf\Upload\Filename:MAX_EXTENSION_LENGTH,RESERVED_WINDOWS_NAMES,BIDI_CONTROLSandCONTROL_CHARACTERS.FileInfo's behaviour is unchanged and itsgetReservedWindowsNames()override seam still works.
With no translator installed, every message is the English string 3.x produced.
To translate them, install a lookup once:
use GravityPdf\Upload\Translation;
Translation::setTranslator(static function (string $text, string $domain): string {
return \__($text, 'my-plugin'); // or your framework's translator
});The English string is the message id, so there is nothing to map, and a lookup that finds
nothing returns what the library would have said anyway. Seed a catalogue from
i18n/upload.pot; no .po or .mo files ship.
In WordPress, merge the catalogue into yours when you extract — wp i18n make-pot never looks
inside vendor/:
wp i18n make-pot . languages/my-plugin.pot \
--merge=vendor/gravitypdf/upload/i18n/upload.potException::getMessage() stays English, including everything Storage\FileSystem throws:
those name the destination and belong in your log. See the
README for the rest, and for getErrorDetails() if
you would rather branch on a code than read prose.
Each of these is listed in full in the changelog.
upload()no longer runs anisValid()override. Both entry points validate through a private method instead, and all three arefinal, so a subclass carrying one is a fatal error when it loads rather than a check that silently stopped running. Validation still runs on everyupload()call, andisValid()is unchanged when you call it yourself. Move a check of your own into aValidationInterface, which all three run.- A
Storage\FileSystemsubclass that overridesresolveFilename()no longer decides which names are refused.upload()applies every refusal to whatever the seam returns:'',.,.., a leading dot, control characters and bidi controls, on top of the device names and blocked extensions it already checked. The shipped seam refused these itself, so nothing changes unless you replaced it. If you did, names it used to store are now refused with'Invalid destination file name'—.htaccessamong them, since a dotfile presents no extension for the deny-list to match. Reproducing the checks in your override is no longer needed, and is harmless if you already do. - The storage collision message changed.
'File already exists'is now'A file named "report.txt" already exists'. The name is sanitized for display first, and a name left with nothing by that reports'A file with that name already exists', so there are two wordings to match rather than one. Update anything matching the old string. Log storage messages rather than showing them to whoever submitted the file: the wording distinguishes a name that exists from a destination that couldn't be created, which is an existence check on your upload directory.getErrors()is the list written to be rendered. - Uploads are written to a temporary name in the destination directory and moved into
place, so no partial content is readable under the final name. Watch for transient
upload-<random>.partentries if you monitor the directory. Withoverwriteoff (the unchanged default) the name is claimed first by an empty file, so a process killed mid-transfer leaves a 0-byte file that collides with the next upload of that name until it's cleared. A failedchmod()now abandons the upload rather than storing the file at the umask's mode. - With
overwriteoff, the existence check and the create are one exclusive operation, so two concurrent requests can't both win the same destination. getHash(),getMimetype(),getSize()andgetDimensions()return''/false/0for an unreadable file instead of raising an error.isValid()resets the error list on each call, so the usualisValid()thenupload()sequence no longer reports every error twice.$file[0] = $valuethrowsInvalidArgumentExceptionunless the value is aFileInfoInterface.- On Windows, a backslash in a client filename is no longer treated as a path separator.
pathinfo()splits on it there and not on POSIX, soa\b.txtwas stored asb.txtunder Windows anda-b.txteverywhere else. It is nowa-b.txton both, which is whatFilename's documented rules always said. A Windows deployment relying on the implicit basename-ing gets longer names than before; nothing on POSIX changes. $file[] = $valueappends. In 3.x it wrote the string key'', so a second append overwrote the first andgetUploadedLocators()came back with a key that is not an offset.Validation\Sizenow throwsInvalidArgumentExceptionfor a bound it cannot use: one that is not anintor a size string, a negative one, or a minimum above the maximum. A float bound previously became aTypeErrorinsidevalidate(), which the collection absorbed and reported to the submitter asValidation could not be completed. Check any bound that comes from configuration —'5M'and5242880are both fine,5.0is not.Validation\Mimetypelowercases its allow-list, asExtensionandFileTypealready did. A list written in any other case previously matched nothing and rejected every upload, so anew Mimetype(['IMAGE/PNG'])that appeared to be rejecting files correctly will now start accepting them. Check any list that is not already lowercase. An entry that is empty once trimmed is dropped from the list rather than matching the''thatgetMimetype()answers for a file it cannot read.Storage\FileSystemrefuses a destination name longer thanFilename::MAX_LENGTH(255 bytes) with'Invalid destination file name', where it used to fail at the write with'Destination file could not be created'. Only reachable from aFileInfoInterfaceof your own; the shippedFileInfotruncates to the budget.- Sanitized filenames are valid UTF-8 where
ext-mbstringis available, so they survivejson_encode()andutf8mb4columns. Without it the guarantee doesn't hold, as in 3.x. - Windows reserved names are matched against the whole extension, so
doc.confkeepsconfwhere 3.x cut it tof. Validation\FileTyperejects an empty string on either side, not just an empty array. A leading dot on an extension is accepted and removed, in bothFileType::allow()andFileSystem::blockExtensions().setExtension()discards an extension longer than 32 bytes (Filename::MAX_EXTENSION_LENGTH).setName()strips\x7Falong with bidi overrides, zero-width marks, line and paragraph separators, and the BOM.- A
$_FILESentry that isn't one of the two shapes the SAPI builds is reported throughgetErrors()rather than warning or raising aTypeError. Only relevant if something other than the SAPI populates$_FILES. - Nothing to change, but new:
FileListtakesFileInfoInterfaceobjects directly, so feeding the library from PSR-7, a worker runtime or a test no longer means faking$_FILES. Two things are yours to answer there, neither a default: overrideFileInfo::isUploadedFile(), and callFileSystem::acceptFilesNotUploadedByPhp(). See Uploads from another source.