Answer the question
In order to leave comments, you need to log in
Yii2 Is there a ready-made extension that would allow uploaded files (photos) to be laid out in a hierarchical folder?
That is, it automatically creates a folder - year, then month, day. Generated an arbitrary name of the uploaded file.
Answer the question
In order to leave comments, you need to log in
This is too simple a task for a standalone extension, but this logic can be implemented as part of an extension.
For an arbitrary name, it is enough to use
And folders are made using fileHelper - www.yiiframework.com/doc-2.0/yii-helpers-basefileh...
Surely you can find a behavior for yii to load a photo that will either be able to do this, or can be customized for 2 lines of code, for example https://github.com/mohorev/yii2-upload-behavior
or dig here
https://github.com/search?utf8=✓&q=yii2+image+behavior
https://github. com/search?utf8=✓&q=yii2+upload+beh...
public static function fromUpload($name, $deleteAfter = true)
{
$webroot = Yii::getAlias('@webroot');
$uploadImage = UploadedFile::getInstanceByName($name);
$instance = new Image();
$instance->ext = pathinfo($uploadImage->name, PATHINFO_EXTENSION);
$instance->title = basename($uploadImage->name);
$instance->size = $uploadImage->size;
if ($instance->size == 0)
return null;
if ($instance->save()) {
$url = strtr('/media/images/{date}/{id}/{file}', ['{date}' => date('Y/m/d'), '{id}' => $instance->id, '{file}' => $uploadImage->name]);
$destPath = $webroot . $url;
FileHelper::createDirectory(dirname($destPath), 0777);
if ($uploadImage->saveAs($destPath, $deleteAfter)) {
list($width, $height) = self::dimension($destPath);
$instance->setAttributes([
'url' => $url,
'width' => $width,
'height' => $height,
'mime' => $uploadImage->type,
'name' => $uploadImage->name,
'title' => '',
]);
$instance->update(false);
return $instance;
} else {
$instance->delete();
return null;
}
}
return null;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question