0

When using below code getting "Direct use of $_FILES Superglobal detected" error. Can anyone suggest how to use in Magento2.

$file = $_FILES["r_dea_license"]["name"]; 
            $array = explode('.', $file);
            $fileName=$array[0];
            $fileExt=$array[1];
            $deaFile='d_'.$fileName."_".time().".".$fileExt;
            $uploader = $this->_fileUploaderFactory->create(['fileId' => 'r_dea_license']);
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(false);
            $uploader->save($media, $deaFile);
0
0

Try this code:

try{
    $uploader = $this->_uploaderFactory->create(['fileId' => 'image']);
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
    $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
    $uploader->setAllowRenameFiles(true);
    $uploader->setFilesDispersion(true);
    $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
        ->getDirectoryRead(DirectoryList::MEDIA);
    $result = $uploader->save($mediaDirectory->getAbsolutePath('gallery/images'));
        if($result['error']==0)
        {
            echo $result['file'];
        }
} catch (\Exception $e) {
    $this->messageManager->addSuccess($e->getMessage());
}
1
  • I need to explode the file $file = $_FILES["r_dea_license"]["name"]; $array = explode('.', $file); $fileName=$array[0]; $fileExt=$array[1]; $deaFile='d_'.$fileName."_".time().".".$fileExt; $uploader = $this->_fileUploaderFactory->create(['fileId' => 'r_dea_license']); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $uploader->save($media, $deaFile); May 22 '18 at 8:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.