Перейти до вмісту
PHP Ecosystem
Explore

Usage examples

Цей контент ще не доступний вашою мовою.

Each example is a standalone PHP snippet that echoes its output, so the documentation site renders it as a live demo. Snippets under the same ## heading share their imports and variables.

For a complete application form with request loading, validation, and storage, use the upload guide.

The widget renders <input class="filepond" id="w0" name="attachment" type="file">, registers the default plugins (File Encode, File Validate Size, File Validate Type, Image EXIF Orientation, and Image Preview), and creates the FilePond instance at the end of the page.

allowImageEdit loads Cropper.js and the editor adapter. imageEditInstantEdit opens the editor as soon as an image is added, and allowImageTransform applies the confirmed crop before the encoded file is submitted.

In an active form, pass the same options without name to the field: $form->field($model, 'avatar')->widget(FilePond::class, [...]).

With allowMultiple, the input name receives the [] suffix (documents[], or Model[documents][] for an active field), and EncodedFile::fromInput() returns one file per entry.

The File Poster plugin renders the existing image inside the item. With type: 'local', the form submits the source identifier (logo.png here) instead of new encoded bytes. Authorize and handle that identifier separately before encoded-upload validation; see stored-image handling.

config keys override typed properties and translated labels, and any JsExpression is emitted as raw JavaScript.

The widget runtime keeps one instance per input id:

const pond = yii2FilePond.get("uploadform-avatar");
pond.addFile("/uploads/sample.png");
yii2FilePond.destroy("uploadform-avatar");

The File Encode plugin submits every file as a JSON string with the name, the client type, the base64 content, and the metadata (such as the crop rectangle of the editor). The snippets of this section build those values by hand, so they run without a browser; in an application they come from $model->load(Yii::$app->request->post()). The upload guide shows that application flow; the snippets below demonstrate the individual helpers.

The validator decodes the payload, so the renamed file is rejected by its real MIME type. extensions checks the client file name while FileSaver derives the stored extension from the content, so mimeTypes is the rule that decides what reaches the disk. Avoid image/* for files stored beneath the web root: it also accepts SVG, which can carry scripts that run on your origin.

Disable the client transform ('allowImageEdit' => true, 'allowImageTransform' => false in the widget) to upload the original image, then apply the crop rectangle of the editor with GD:

Configure the three positive uploads limits from the application’s measured processing budget; see server cropping for the policy and worker-memory example. Source bytes, dimensions, and pixel counts are checked before decoding, including when the requested output crop is small. ImageCropper::crop() returns the file unchanged without decoding when no rectangle is present, rotates JPEG images by their EXIF orientation before cropping, and keeps PNG transparency.

With a base name, the first file keeps it and the following ones receive a numeric suffix (order-42.pdf, order-42-1.pdf). Store uploads outside the web root unless they must be served directly.