Repository navigation
Local Adapter
github-actions[bot] edited this page Nov 24, 2026
·
2 revisions
InitPHP\Upload\Adapters\LocalAdapter stores files on the local filesystem.
It is the simplest adapter and needs no extensions beyond ext-fileinfo.
| Key | Type | Default | Description |
|---|---|---|---|
dir |
string |
'' |
Absolute base directory files are written into. |
url |
string |
'' |
Public base URL the directory is served from; used to build getURL(). |
use InitPHP\Upload\Upload;
use InitPHP\Upload\File;
use InitPHP\Upload\Adapters\LocalAdapter;
$adapter = new LocalAdapter(
[
'dir' => '/var/app/storage/uploads/', // outside the web root
'url' => 'https://example.com/uploads/', // served read-only, scripts never executed
],
[
'allowed_extensions' => ['jpg', 'png', 'pdf'],
'allowed_mime_types' => ['image/jpeg', 'image/png', 'application/pdf'], // real type, via finfo
'allowed_max_size' => 5 * 1024 * 1024,
]
);
$upload = new Upload($adapter);dir should point outside the web root — see
Security Best Practices.
foreach (File::setPost('documents') as $file) {
// Never store under the client's file name: generate one (the extension is kept).
// Two uploads with the same client name would otherwise overwrite each other.
$file->rename(bin2hex(random_bytes(16)));
$stored = $upload->setFile($file)->to();
if ($stored !== false) {
echo htmlspecialchars((string) $stored->getURL(), ENT_QUOTES, 'UTF-8'); // https://example.com/uploads/<32hex>.pdf
}
}The $target argument is a path prefix appended under dir. Missing
directories are created automatically and recursively:
$upload->setFile($file)->to('invoices/2026/06');
// file: <dir>/invoices/2026/06/<name>
// URL: https://example.com/uploads/invoices/2026/06/<name>The adapter chooses how to place the file based on its source:
Source of the File
|
Action |
|---|---|
A real HTTP upload (File::setPost()) |
moved with move_uploaded_file()
|
A path-loaded file (File::setPath()) |
copied (the original stays put) |
$file = File::setPath('/var/assets/logo.png');
$upload->setFile($file)->to();
// /var/assets/logo.png still exists; a copy now lives under `dir`.This distinction matters: move_uploaded_file() only works on genuine POST
uploads, so copying is the correct behaviour for files you loaded yourself.
$file = File::setPost('avatar')[0];
$file->rename('user-' . $userId); // keeps the original extension
$stored = $upload->setFile($file)->to('avatars');
// stored at <dir>/avatars/user-42.jpg| Outcome | Result |
|---|---|
| Stored successfully | the File, with getURL() set |
| The move/copy returned false (e.g. not writable) | false |
| Validation failed | throws UploadException
|
| Destination directory could not be created | throws UploadException
|
use InitPHP\Upload\Exceptions\UploadException;
try {
$result = $upload->setFile($file)->to();
$ok = $result !== false;
} catch (UploadException $e) {
// validation failed, or the directory could not be created
$message = $e->getMessage();
}-
Permissions. The PHP process must be able to write to
dir. Newly created sub-directories are made with mode0777(subject to your umask). -
Trailing slashes in
dirandurlare handled for you —'/uploads'and'/uploads/'behave identically. -
Serving files.
urlis only used to compute the public URL string; the package does not serve the files. Pointurlat wherever your web server exposesdir.
- Validation · Security Best Practices
- Recipes — unique names, avatars, galleries
- FTP Adapter · S3 Adapter
initphp/upload · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Reference
Adapters
Practical Guides
Other