Upload files with cakephp 4.2

If you don’t want to include WWW_ROOT in the path you save in the entity, then don’t include it. You’re the one that’s creating the string you’re saving, you can put whatever you want there. You just have to make sure that when you reference it in other places, you add that back in.

Other notes:

You are moving the submitted file only if it’s one of a few media types, but you’re saving the entity before that check. If I upload a PDF, you will have a row in your database that refers to a file you don’t have in your filesystem.

You are accepting the user’s filename to save it under. If somebody else has uploaded a file called “IMG0001.jpg”, and I upload one with the same name, there will be a collision and one of the files will be lost.