3

I'm checking condition like file uploaded or not, for that I used $_FILES but as magento 2 coding standards we cannot use $_FILES.

if ($id) {
    $model->load($id);
}

//if (isset($_FILES['file']) && $_FILES['file']['name'] != '') {
    $imageFile = $this->helper->uploadFile('file');
    $model->setFile($imageFile);
//}

So I created helper class to upload file

public function uploadFile($scope)
{   
   try {
        $uploader = $this->_fileUploaderFactory->create(['fileId' => $scope]);
        $uploader->setAllowRenameFiles(true);
        $uploader->setFilesDispersion(true);
        $uploader->setAllowCreateFolders(true);

        if ($uploader->save($this->getBaseDir())) {
            return $uploader->getUploadedFileName();    
        }

    } catch (\Exception $e) { 
        throw new $e('File was not uploaded');
    }
}

But when I have no upload any file it's show fatal error "File was not uploaded". How we check that file was selected in field or not?

4 Answers 4

5

This will resolve the problem. Put this in controller.

$files = $this->getRequest()->getFiles()

Here is Magento2 :: Accessing $_FILES using Magento Framework

3

First get saved file string from model and save it to one variable

if ($id) {
    $model->load($id);
    $uploadedFile = $model->getFile(); // Get already uploded file path
}

Now pass it into your helper function

$imageFile = $this->helper->uploadFile('file', $uploadedFile);
$model->setFile($imageFile);

Now in helper function remove throw new exception from catch and return $uploadedFile variable.

public function uploadFile($scope, $uploadedFile)
{   
   try {
        $uploader = $this->_fileUploaderFactory->create(['fileId' => $scope]);
        $uploader->setAllowRenameFiles(true);
        $uploader->setFilesDispersion(true);
        $uploader->setAllowCreateFolders(true);

        if ($uploader->save($this->getBaseDir())) {
            return $uploader->getUploadedFileName();    
        }

    } catch (\Exception $e) { }

    return $uploadedFile;
}

For this trick, if you have not select file in file uploader, Old value will save in database. If you have select or upload new file than new file will be saved in database.

1

Here is a trick :)

File uploader class uses \Magento\Framework\File\Uploader, there in constructor it calls a method to validate if file was uploaded and throws exception.

So if you put a try catch in below line you can check if file was loaded or not:

at this line => $uploader = $this->_fileUploaderFactory->create(['fileId' => $scope]);

So you can put try and catch along with it. eg:

try{
    $uploader = $this->_fileUploaderFactory->create(['fileId' => $scope]);
} catch(\Exception $e) {
    //file was not uploaded
}

At this stage fileuploader will only call constructor of \Magento\MediaStorage\Model\File\Uploader class which extends \Magento\Framework\File\Uploader and that will do the trick.

0

every time when file not selected in the input field you will get the Exception, this is prevent event on empty $_FILES fields so if you don't wanna get Exception you should remove "throw" function and this will be look like:

catch (\Exception $e) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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