• 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
آپلود فایل با زند ۲
#1
سلام

من برای آپلود فایل همچین کدی نوشتم :

کد پی‌اچ‌پی:
$upload = new \Zend\File\Transfer\Transfer();
                
$upload->setDestination($destination);
                     
$upload->addValidators(array(
                           array(
'Upload'true),
                           array(
'IsImage'true),
                           array(
'Size'100000000),
                     ));
                     
$upload->receive($values['image']); 

$values['image'] اسم فایل آپلود شدست که از فرم گرفته میشه و $destination هم مسیری که فایل باید بره اونجا

طبق چیزی که من از کد های Transfer سردرآوردم این باید درست کار کنه ولی متاسفانه مشکل داره و چیزی آپلود نمیشه وقتی هم با isValid میخوام چکش کنم ولید نیست

کسی میتونه راهنمایی کنه؟
  پاسخ
تشکر شده توسط :
#2
ورودی تابع رسیو به جای نام فایل آرایه $_FILES رو بده ببین کار می کنه یا نه
  پاسخ
تشکر شده توسط :
#3
متاسفانه کار نکرد. این خروجی رو با دستور print_r میگیرم

کد:
Zend\File\Transfer\Transfer Object
(
    [adapter:protected] => Array
        (
            [0] => Zend\File\Transfer\Adapter\Http Object
                (
                    [break:protected] => Array
                        (
                            [Zend\Validator\File\Upload] => 1
                            [Zend\Validator\File\IsImage] => 1
                            [Zend\Validator\File\Size] => 100000000
                        )

                    [filterManager:protected] =>
                    [filters:protected] => Array
                        (
                        )

                    [loaders:protected] => Array
                        (
                        )

                    [messages:protected] => Array
                        (
                            [fileUploadErrorIniSize] => File '2.jpg' exceeds the defined ini size
                        )

                    [translator:protected] =>
                    [translatorEnabled:protected] => 1
                    [translatorTextDomain:protected] => default
                    [validatorManager:protected] => Zend\File\Transfer\Adapter\ValidatorPluginManager Object
                        (
                            [aliases:protected] => Array
                                (
                                    [count] => filecount
                                    [crc32] => filecrc32
                                    [excludeextension] => fileexcludeextension
                                    [excludemimetype] => fileexcludemimetype
                                    [exists] => fileexists
                                    [extension] => fileextension
                                    [filessize] => filefilessize
                                    [hash] => filehash
                                    [imagesize] => fileimagesize
                                    [iscompressed] => fileiscompressed
                                    [isimage] => fileisimage
                                    [md5] => filemd5
                                    [mimetype] => filemimetype
                                    [notexists] => filenotexists
                                    [sha1] => filesha1
                                    [size] => filesize
                                    [upload] => fileupload
                                    [wordcount] => filewordcount
                                )

                            [invokableClasses:protected] => Array
                                (
                                    [alnum] => Zend\I18n\Validator\Alnum
                                    [alpha] => Zend\I18n\Validator\Alpha
                                    [barcodecode25interleaved] => Zend\Validator\Barcode\Code25interleaved
                                    [barcodecode25] => Zend\Validator\Barcode\Code25
                                    [barcodecode39ext] => Zend\Validator\Barcode\Code39ext
                                    [barcodecode39] => Zend\Validator\Barcode\Code39
                                    [barcodecode93ext] => Zend\Validator\Barcode\Code93ext
                                    [barcodecode93] => Zend\Validator\Barcode\Code93
                                    [barcodeean12] => Zend\Validator\Barcode\Ean12
                                    [barcodeean13] => Zend\Validator\Barcode\Ean13
                                    [barcodeean14] => Zend\Validator\Barcode\Ean14
                                    [barcodeean18] => Zend\Validator\Barcode\Ean18
                                    [barcodeean2] => Zend\Validator\Barcode\Ean2
                                    [barcodeean5] => Zend\Validator\Barcode\Ean5
                                    [barcodeean8] => Zend\Validator\Barcode\Ean8
                                    [barcodegtin12] => Zend\Validator\Barcode\Gtin12
                                    [barcodegtin13] => Zend\Validator\Barcode\Gtin13
                                    [barcodegtin14] => Zend\Validator\Barcode\Gtin14
                                    [barcodeidentcode] => Zend\Validator\Barcode\Identcode
                                    [barcodeintelligentmail] => Zend\Validator\Barcode\Intelligentmail
                                    [barcodeissn] => Zend\Validator\Barcode\Issn
                                    [barcodeitf14] => Zend\Validator\Barcode\Itf14
                                    [barcodeleitcode] => Zend\Validator\Barcode\Leitcode
                                    [barcodeplanet] => Zend\Validator\Barcode\Planet
                                    [barcodepostnet] => Zend\Validator\Barcode\Postnet
                                    [barcoderoyalmail] => Zend\Validator\Barcode\Royalmail
                                    [barcodesscc] => Zend\Validator\Barcode\Sscc
                                    [barcodeupca] => Zend\Validator\Barcode\Upca
                                    [barcodeupce] => Zend\Validator\Barcode\Upce
                                    [barcode] => Zend\Validator\Barcode
                                    [between] => Zend\Validator\Between
                                    [callback] => Zend\Validator\Callback
                                    [creditcard] => Zend\Validator\CreditCard
                                    [csrf] => Zend\Validator\Csrf
                                    [date] => Zend\Validator\Date
                                    [datestep] => Zend\Validator\DateStep
                                    [dbnorecordexists] => Zend\Validator\Db\NoRecordExists
                                    [dbrecordexists] => Zend\Validator\Db\RecordExists
                                    [digits] => Zend\Validator\Digits
                                    [emailaddress] => Zend\Validator\EmailAddress
                                    [explode] => Zend\Validator\Explode
                                    [filecount] => Zend\Validator\File\Count
                                    [filecrc32] => Zend\Validator\File\Crc32
                                    [fileexcludeextension] => Zend\Validator\File\ExcludeExtension
                                    [fileexcludemimetype] => Zend\Validator\File\ExcludeMimeType
                                    [fileexists] => Zend\Validator\File\Exists
                                    [fileextension] => Zend\Validator\File\Extension
                                    [filefilessize] => Zend\Validator\File\FilesSize
                                    [filehash] => Zend\Validator\File\Hash
                                    [fileimagesize] => Zend\Validator\File\ImageSize
                                    [fileiscompressed] => Zend\Validator\File\IsCompressed
                                    [fileisimage] => Zend\Validator\File\IsImage
                                    [filemd5] => Zend\Validator\File\Md5
                                    [filemimetype] => Zend\Validator\File\MimeType
                                    [filenotexists] => Zend\Validator\File\NotExists
                                    [filesha1] => Zend\Validator\File\Sha1
                                    [filesize] => Zend\Validator\File\Size
                                    [fileupload] => Zend\Validator\File\Upload
                                    [filewordcount] => Zend\Validator\File\WordCount
                                    [float] => Zend\I18n\Validator\Float
                                    [greaterthan] => Zend\Validator\GreaterThan
                                    [hex] => Zend\Validator\Hex
                                    [hostname] => Zend\Validator\Hostname
                                    [iban] => Zend\I18n\Validator\Iban
                                    [identical] => Zend\Validator\Identical
                                    [inarray] => Zend\Validator\InArray
                                    [int] => Zend\I18n\Validator\Int
                                    [ip] => Zend\Validator\Ip
                                    [isbn] => Zend\Validator\Isbn
                                    [lessthan] => Zend\Validator\LessThan
                                    [notempty] => Zend\Validator\NotEmpty
                                    [postcode] => Zend\I18n\Validator\PostCode
                                    [regex] => Zend\Validator\Regex
                                    [sitemapchangefreq] => Zend\Validator\Sitemap\Changefreq
                                    [sitemaplastmod] => Zend\Validator\Sitemap\Lastmod
                                    [sitemaploc] => Zend\Validator\Sitemap\Loc
                                    [sitemappriority] => Zend\Validator\Sitemap\Priority
                                    [stringlength] => Zend\Validator\StringLength
                                    [step] => Zend\Validator\Step
                                    [uri] => Zend\Validator\Uri
                                )

                            [shareByDefault:protected] =>
                            [allowOverride:protected] => 1
                            [autoAddInvokableClass:protected] => 1
                            [creationOptions:protected] =>
                            [serviceLocator:protected] =>
                            [canonicalNames:protected] => Array
                                (
                                    [Upload] => upload
                                    [upload] => upload
                                    [fileupload] => fileupload
                                    [IsImage] => isimage
                                    [isimage] => isimage
                                    [fileisimage] => fileisimage
                                    [Size] => size
                                    [size] => size
                                    [filesize] => filesize
                                )

                            [factories:protected] => Array
                                (
                                )

                            [abstractFactories:protected] => Array
                                (
                                )

                            [pendingAbstractFactoryRequests:protected] => Array
                                (
                                )

                            [shared:protected] => Array
                                (
                                )

                            [instances:protected] => Array
                                (
                                )

                            [initializers:protected] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Zend\File\Transfer\Adapter\ValidatorPluginManager Object
*RECURSION*
                                            [1] => injectTranslator
                                        )

                                    [1] => Closure Object
                                        (
                                            [static] => Array
                                                (
                                                    [self] => Zend\File\Transfer\Adapter\ValidatorPluginManager Object
*RECURSION*
                                                )

                                            [this] => Zend\File\Transfer\Adapter\ValidatorPluginManager Object
*RECURSION*
                                            [parameter] => Array
                                                (
                                                    [$instance] =>
                                                )

                                        )

                                )

                            [peeringServiceManagers:protected] => Array
                                (
                                )

                            [retrieveFromPeeringManagerFirst:protected] =>
                            [throwExceptionInCreate:protected] => 1
                            [canonicalNamesReplacements:protected] => Array
                                (
                                    [-] =>
                                    [_] =>
                                    [ ] =>
                                    [\] =>
                                    [/] =>
                                )

                        )

                    [validators:protected] => Array
                        (
                            [Zend\Validator\File\Upload] => Zend\Validator\File\Upload Object
                                (
                                    [messageTemplates:protected] => Array
                                        (
                                            [fileUploadErrorIniSize] => File '%value%' exceeds the defined ini size
                                            [fileUploadErrorFormSize] => File '%value%' exceeds the defined form size
                                            [fileUploadErrorPartial] => File '%value%' was only partially uploaded
                                            [fileUploadErrorNoFile] => File '%value%' was not uploaded
                                            [fileUploadErrorNoTmpDir] => No temporary directory was found for file '%value%'
                                            [fileUploadErrorCantWrite] => File '%value%' can't be written
                                            [fileUploadErrorExtension] => A PHP extension returned an error while uploading the file '%value%'
                                            [fileUploadErrorAttack] => File '%value%' was illegally uploaded. This could be a possible attack
                                            [fileUploadErrorFileNotFound] => File '%value%' was not found
                                            [fileUploadErrorUnknown] => Unknown error while uploading file '%value%'
                                        )

                                    [options:protected] => Array
                                        (
                                            [files] => Array
                                                (
                                                    [2.jpg] => Array
                                                        (
                                                            [name] => 2.jpg
                                                            [error] => 1
                                                        )

                                                )

                                        )

                                    [value:protected] => 2.jpg
                                    [abstractOptions:protected] => Array
                                        (
                                            [messages] => Array
                                                (
                                                    [fileUploadErrorIniSize] => File '2.jpg' exceeds the defined ini size
                                                )

                                            [messageTemplates] => Array
                                                (
                                                    [fileUploadErrorIniSize] => File '%value%' exceeds the defined ini size
                                                    [fileUploadErrorFormSize] => File '%value%' exceeds the defined form size
                                                    [fileUploadErrorPartial] => File '%value%' was only partially uploaded
                                                    [fileUploadErrorNoFile] => File '%value%' was not uploaded
                                                    [fileUploadErrorNoTmpDir] => No temporary directory was found for file '%value%'
                                                    [fileUploadErrorCantWrite] => File '%value%' can't be written
                                                    [fileUploadErrorExtension] => A PHP extension returned an error while uploading the file '%value%'
                                                    [fileUploadErrorAttack] => File '%value%' was illegally uploaded. This could be a possible attack
                                                    [fileUploadErrorFileNotFound] => File '%value%' was not found
                                                    [fileUploadErrorUnknown] => Unknown error while uploading file '%value%'
                                                )

                                            [messageVariables] => Array
                                                (
                                                )

                                            [translator] =>
                                            [translatorTextDomain] =>
                                            [translatorEnabled] => 1
                                            [valueObscured] =>
                                        )

                                )

                            [Zend\Validator\File\IsImage] => Zend\Validator\File\IsImage Object
                                (
                                    [messageTemplates:protected] => Array
                                        (
                                            [fileIsImageFalseType] => File '%value%' is no image, '%type%' detected
                                            [fileIsImageNotDetected] => The mimetype of file '%value%' could not be detected
                                            [fileIsImageNotReadable] => File '%value%' is not readable or does not exist
                                        )

                                    [messageVariables:protected] => Array
                                        (
                                            [type] => type
                                        )

                                    [type:protected] =>
                                    [finfo:protected] =>
                                    [magicFiles:protected] => Array
                                        (
                                            [0] => /usr/share/misc/magic
                                            [1] => /usr/share/misc/magic.mime
                                            [2] => /usr/share/misc/magic.mgc
                                            [3] => /usr/share/mime/magic
                                            [4] => /usr/share/mime/magic.mime
                                            [5] => /usr/share/mime/magic.mgc
                                            [6] => /usr/share/file/magic
                                            [7] => /usr/share/file/magic.mime
                                            [8] => /usr/share/file/magic.mgc
                                        )

                                    [options:protected] => Array
                                        (
                                            [enableHeaderCheck] =>
                                            [disableMagicFile] =>
                                            [magicFile] =>
                                            [mimeType] => application/cdf,application/dicom,application/fractals,application/postscript,application/vnd.hp-hpgl,application/vnd.oasis.opendocument.graphics,application/x-cdf,application/x-cmu-raster,application/x-ima,application/x-inventor,application/x-koan,application/x-portable-anymap,application/x-world-x-3dmf,image/bmp,image/c,image/cgm,image/fif,image/gif,image/jpeg,image/jpm,image/jpx,image/jp2,image/naplps,image/pjpeg,image/png,image/svg,image/svg+xml,image/tiff,image/vnd.adobe.photoshop,image/vnd.djvu,image/vnd.fpx,image/vnd.net-fpx,image/x-cmu-raster,image/x-cmx,image/x-coreldraw,image/x-cpi,image/x-emf,image/x-ico,image/x-icon,image/x-jg,image/x-ms-bmp,image/x-niff,image/x-pict,image/x-pcx,image/x-png,image/x-portable-anymap,image/x-portable-bitmap,image/x-portable-greymap,image/x-portable-pixmap,image/x-quicktime,image/x-rgb,image/x-tiff,image/x-unknown,image/x-windows-bmp,image/x-xpmi
                                        )

                                    [value:protected] =>
                                    [abstractOptions:protected] => Array
                                        (
                                            [messages] => Array
                                                (
                                                )

                                            [messageTemplates] => Array
                                                (
                                                    [fileIsImageFalseType] => File '%value%' is no image, '%type%' detected
                                                    [fileIsImageNotDetected] => The mimetype of file '%value%' could not be detected
                                                    [fileIsImageNotReadable] => File '%value%' is not readable or does not exist
                                                )

                                            [messageVariables] => Array
                                                (
                                                    [type] => type
                                                )

                                            [translator] =>
                                            [translatorTextDomain] =>
                                            [translatorEnabled] => 1
                                            [valueObscured] =>
                                        )

                                )

                            [Zend\Validator\File\Size] => Zend\Validator\File\Size Object
                                (
                                    [messageTemplates:protected] => Array
                                        (
                                            [fileSizeTooBig] => Maximum allowed size for file '%value%' is '%max%' but '%size%' detected
                                            [fileSizeTooSmall] => Minimum expected size for file '%value%' is '%min%' but '%size%' detected
                                            [fileSizeNotFound] => File '%value%' is not readable or does not exist
                                        )

                                    [messageVariables:protected] => Array
                                        (
                                            [min] => Array
                                                (
                                                    [options] => min
                                                )

                                            [max] => Array
                                                (
                                                    [options] => max
                                                )

                                            [size] => size
                                        )

                                    [size:protected] =>
                                    [options:protected] => Array
                                        (
                                            [min] =>
                                            [max] =>
                                            [useByteString] => 1
                                        )

                                    [value:protected] =>
                                    [abstractOptions:protected] => Array
                                        (
                                            [messages] => Array
                                                (
                                                )

                                            [messageTemplates] => Array
                                                (
                                                    [fileSizeTooBig] => Maximum allowed size for file '%value%' is '%max%' but '%size%' detected
                                                    [fileSizeTooSmall] => Minimum expected size for file '%value%' is '%min%' but '%size%' detected
                                                    [fileSizeNotFound] => File '%value%' is not readable or does not exist
                                                )

                                            [messageVariables] => Array
                                                (
                                                    [min] => Array
                                                        (
                                                            [options] => min
                                                        )

                                                    [max] => Array
                                                        (
                                                            [options] => max
                                                        )

                                                    [size] => size
                                                )

                                            [translator] =>
                                            [translatorTextDomain] =>
                                            [translatorEnabled] => 1
                                            [valueObscured] =>
                                        )

                                )

                        )

                    [files:protected] => Array
                        (
                        )

                    [tmpDir:protected] =>
                    [options:protected] => Array
                        (
                            [ignoreNoFile] =>
                            [useByteString] => 1
                            [magicFile] =>
                            [detectInfos] => 1
                        )

                )

        )

)
در اینجا اسم فایل هم 2.jpg هست.

اینم بگم که فرم رو هم اینطوری ست کردم :

کد پی‌اچ‌پی:
$this->add(array(
            
'name'          => 'image',
            
'options'       => array(
                
'label' => __('Image'),
            ),
            
'attributes'    => array(
                
'type'  => 'file',
                
'description'   => '',
                
            )
        )); 

برای ولید مردنش هم این
کد پی‌اچ‌پی:
$this->add(array(
            
'name'      => 'image',
            
'required'  => false,
        )); 
  پاسخ
تشکر شده توسط :
#4
پروپرتی files خالیه. باید پر باشه. enctype="multipart/form-data" روی تگ فرم قرار دادی؟
  پاسخ
تشکر شده توسط :
#5
مشکل حل شد. هم برای فرم enctype="multipart/form-data" رو قرار دادم و هم اینکه من توی این فرم در اصلی دوتا فیلد برای آپلود داشتم. توی تست ها فقط یکیش رو پر میکردم که خطا میداد.

یه مشکل دیگه هم دارم با این کد های آپلود. من ی فیلتر اد میکنم برای رینمی کردن فایل . یه چیزی شبیه به این :
کد پی‌اچ‌پی:
$upload->addFilter('Rename'$this->upload()->rename($file['image']['name'], 'image')); 

اگر از این فیلتر استفاده نکنم خیلی راحت دو تا فایل دو تا فیلد آپلود میشن و میرن توی پوشه مشخص شده و اسمشون رو هم میتونم بگیرم و توی دیتابیس ذخیره کنم. ولی وقتی میخوام با این فیلتر فایل رو رینیم کنم. متاسفانه فقط یکی از فایل ها آپلود و ری نیم میشه

خیلی روش ها رو تست کردم و نتونستم نتیجه بگیرم. مثلا سعی کردم با foreach آرایه فایل رو پردازش کنم و برای هر فایل جداگانه فیلتر رو اعمال کنم و یا به جای addFilter از addFilters استفاده کنم و ... که نتونستم تنظیمش کنم.
  پاسخ
تشکر شده توسط :
#6
(۱۳۹۱ مرداد ۳۰, ۰۹:۱۶ ب.ظ)voltan نوشته: مشکل حل شد. هم برای فرم enctype="multipart/form-data" رو قرار دادم و هم اینکه من توی این فرم در اصلی دوتا فیلد برای آپلود داشتم. توی تست ها فقط یکیش رو پر میکردم که خطا میداد.

یه مشکل دیگه هم دارم با این کد های آپلود. من ی فیلتر اد میکنم برای رینمی کردن فایل . یه چیزی شبیه به این :
کد پی‌اچ‌پی:
$upload->addFilter('Rename'$this->upload()->rename($file['image']['name'], 'image')); 

اگر از این فیلتر استفاده نکنم خیلی راحت دو تا فایل دو تا فیلد آپلود میشن و میرن توی پوشه مشخص شده و اسمشون رو هم میتونم بگیرم و توی دیتابیس ذخیره کنم. ولی وقتی میخوام با این فیلتر فایل رو رینیم کنم. متاسفانه فقط یکی از فایل ها آپلود و ری نیم میشه

خیلی روش ها رو تست کردم و نتونستم نتیجه بگیرم. مثلا سعی کردم با foreach آرایه فایل رو پردازش کنم و برای هر فایل جداگانه فیلتر رو اعمال کنم و یا به جای addFilter از addFilters استفاده کنم و ... که نتونستم تنظیمش کنم.


متاسفانه من هنوز از فرم آپلود zf2 استفاده نکردم ولی تا جایی که می دونم فیلتر رو باید برای هر کدوم از فایلهات تعریف کنی
  پاسخ
تشکر شده توسط : voltan


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان