To retrieve files you can use the
$mediaItems = $newsItem->getMedia();
The method returns a collection of
You can retrieve the url and path to the file associated with the
$publicUrl = $mediaItems->getUrl();
$fullPathOnDisk = $mediaItems->getPath();
An instance of
Media also has a name, by default its filename:
echo $mediaItems->name; // Display the name
$mediaItems->name = 'new name';
$mediaItems->save(); // The new name gets saved. Activerecord ftw!
The name of a
Media instance can be changed when it's added to the medialibrary:
The name of the uploaded file can be changed via the media-object:
$mediaItems->file_name = 'newFileName.jpg';
$mediaItems->save(); // Saving will also rename the file on the filesystem.
The name of the uploaded file can also be changed when it gets added to the media-library:
You can also retrieve the size of the file via
$mediaItems->size; // Returns the size in bytes
$mediaItems->human_readable_size; // Returns the size in a human readable format (eg. 1,5 MB)
An instance of
Media also contains the mime type of the file.
$mediaItems->mime_type; // Returns the mime type
You can remove something from the library by simply calling
delete on an instance of
Media instance gets deleted all related files will be removed from the filesystem.
Deleting a model with associated media, will also delete all associated files.
$newsItem->delete(); // all associated files will be deleted as well
If you want to remove all associated media in a specific collection you can use the
$newsItem->clearMediaCollection(); // all media will be deleted