How to check if $uploaded_file->moveTo($destination.$fileName) was successful

I try to check if file upload was successfull.
At the moment I use

  if (empty($uploaded_file->moveTo($destination.$fileName))) {...}

Can anyone tell if this is the right way to check this, or show me a link where I can find further information?



moveTo() doesn’t have a return value.
See laminas-diactoros/UploadedFile.php at 2.9.x · laminas/laminas-diactoros · GitHub

You will need to surround that function with a try-catch block and catch exceptions accordingly to do what you want to do if it fails.

thanks for the answer


it may not be as elegant as a try/catch - but maybe sufficient enough - to do a

if(is_file($destination.$fileName)) { ... }

after your move. :slight_smile:

Thanks for the idea!

if it’s throwing an exception, an if statement doesn’t seem like it’s going to be useful.

UploadedFileInterface::moveTo() doesn’t have a return value. The operation will either be successful or throw an exception. So you need to use a try / catch block if you want to handle the failure.