ستساعدك المقالة التالية: كيفية إضافة حقل الصورة ومعاينة الصورة في نموذج مكون واجهة المستخدم الإدارية Magento 2
<؟ php
Vendor \ Module \ Model مساحة الاسم؛
استخدم Magento \ MediaStorage \ Helper \ File \ Storage \ Database ؛
استخدام Magento \ Framework \ Filesystem ؛
استخدم Magento \ MediaStorage \ Model \ File \ UploaderFactory ؛
استخدام Magento \ Store \ Model \ StoreManagerInterface ؛
استخدام Psr \ Log \ LoggerInterface ؛
فئة ImageUploader
{
خاص $ coreFileStorageDatabase؛
دليل mediaDirect $ الخاص ؛
الخاص $ uploaderFactory؛
$ storeManager الخاص ؛
المسجل $ الخاص؛
العامة baseTmpPath $ ؛
قاعدة عامة $ basePath ؛
$ العام المسموح به الإضافات؛
الوظيفة العامة __construct (
قاعدة البيانات $ coreFileStorageDatabase ،
نظام الملفات $ نظام الملفات ،
UploaderFactory $ uploaderFactory،
StoreManagerInterface $ storeManager ،
LoggerInterface $ المسجل
)
{
$ this-> coreFileStorageDatabase = $ coreFileStorageDatabase ؛
$ this-> mediaDirectory = $ filesystem-> getDirectoryWrite (\ Magento \ Framework \ App \ Filesystem \ DirectoryList :: MEDIA) ؛
$ this-> uploaderFactory = $ uploaderFactory؛
$ this-> storeManager = $ storeManager ؛
$ this-> المسجل = $ المسجل ؛
$ this-> baseTmpPath = “label / icon” ؛
$ this-> basePath = “label / icon” ؛
$ this-> allowExtensions = [‘jpg’, ‘jpeg’, ‘gif’, ‘png’]؛
}
setBaseTmpPath للوظائف العامة ($ baseTmpPath)
{
$ this-> baseTmpPath = $ baseTmpPath ؛
}
تعيين الوظيفة العامة (basePath $)
{
$ this-> basePath = $ basePath ؛
}
setAllowedExtensions ($ allowedExtensions) الوظيفة العامة
{
$ this-> allowExtensions = $ allowedExtensions؛
}
الوظيفة العامة getBaseTmpPath ()
{
إرجاع $ this-> baseTmpPath ؛
}
الوظيفة العامة getBasePath ()
{
إرجاع $ this-> basePath ؛
}
getAllowedExtensions () الوظيفة العامة
{
إرجاع $ this-> allowedExtensions؛
}
getFilePath الوظيفة العامة ($ path، $ imageName)
{
إرجاع rtrim ($ path، ‘/’). “/”. ltrim ($ imageName، ‘/’)؛
}
الوظيفة العامة moveFileFromTmp ($ imageName)
{
$ baseTmpPath = $ this-> getBaseTmpPath ()؛
$ basePath = $ this-> getBasePath ()؛
$ baseImagePath = $ this-> getFilePath ($ basePath، $ imageName)؛
$ baseTmpImagePath = $ this-> getFilePath ($ baseTmpPath، $ imageName)؛
يحاول {
$ this-> coreFileStorageDatabase-> copyFile (
baseTmpImagePath $ ،
$ baseImagePath
) ؛
$ this-> mediaDirectory-> إعادة تسمية الملف (
baseTmpImagePath $ ،
$ baseImagePath
) ؛
} catch (\ استثناء $ e) {
رمي الجديد \ Magento \ Framework \ Exception \ LocalizedException (
__ (“حدث خطأ ما أثناء حفظ الملف (الملفات).”)
) ؛
}
إرجاع $ imageName؛
}
الوظيفة العامة saveFileToTmpDir ($ fileId)
{
$ baseTmpPath = $ this-> getBaseTmpPath ()؛
$ uploader = $ this-> uploaderFactory-> إنشاء ([‘fileId’ => $fileId]) ؛
$ uploader-> setAllowedExtensions ($ this-> getAllowedExtensions ())؛
$ uploader-> setAllowRenameFiles (صحيح) ؛
نتيجة $ = $ uploader-> save ($ this-> mediaDirectory-> getAbsolutePath ($ baseTmpPath)) ؛
إذا (! $ result) {
رمي الجديد \ Magento \ Framework \ Exception \ LocalizedException (
__ (“لا يمكن حفظ الملف في المجلد الوجهة.”)
) ؛
}
نتيجة $[‘tmp_name’] = str_replace (‘\\’، ‘/’، $ نتيجة[‘tmp_name’]) ؛
نتيجة $[‘path’] = str_replace (‘\\’، ‘/’، $ نتيجة[‘path’]) ؛
نتيجة $[‘url’] = $ this-> storeManager
-> getStore ()
-> getBaseUrl (
\ Magento \ Framework \ UrlInterface :: URL_TYPE_MEDIA
). $ this-> getFilePath ($ baseTmpPath ، $ result[‘file’]) ؛
نتيجة $[‘name’] = النتيجة بالدولار[‘file’]؛
if (isset ($ result[‘file’])) {
يحاول {
$ نسبياPath = rtrim ($ baseTmpPath، ‘/’). “/”. ltrim (نتيجة $[‘file’]، ‘/’)؛
$ this-> coreFileStorageDatabase-> saveFile ($ dynamicPath) ؛
} catch (\ استثناء $ e) {
$ this-> المسجل-> حرج ($ e) ؛
رمي الجديد \ Magento \ Framework \ Exception \ LocalizedException (
__ (“حدث خطأ ما أثناء حفظ الملف (الملفات).”)
) ؛
}
}
إرجاع نتيجة $؛
}
}