Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MAQE's avatar
Level 8

Testing file upload

Hi,

I tried to test file upload by using \Symfony\Component\HttpFoundation\File\UploadedFile

I created the instance by using:

$file = new \Symfony\Component\HttpFoundation\File\UploadedFile(base_path('resources/data/test.jpg'),'test.jpg');

However, when the instance is created, it seems the size can't be read, I dd() it, and it showed

Symfony\Component\HttpFoundation\File\UploadedFile {#494
  -test: false
  -originalName: "test.jpg"
  -mimeType: "application/octet-stream"
  -size: null
  -error: 0
}

And, the file was null when it was read by the API.

Any idea, what's wrong with this?

PS. I migrated the tests from L4.2, the codes work properly before I moved to L5.1

0 likes
2 replies
phildawson's avatar
Level 26

You would need to pass it a fourth param of the size to have it set. It only showing 'application/octet-stream' as the mime as that is a default if null is passed. The params are passed to it from $_FILES usually.

"Accepts the information of the uploaded file as provided by the PHP global $_FILES."

1 like
MAQE's avatar
Level 8

@phildawson

Thanks a lot for your answer. After digging for a while, I ended up with filling-up every constructor's argument as follow:

$path = base_path('resources/data/test.png');
$file = new UploadedFile($path, 'test.png', 'image/png', filesize($path), UPLOAD_ERR_OK, true);

I wonder that why in L4, I didn't have to fill-in every argument just the path and filename, you have any idea on this?

Please or to participate in this conversation.