Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

kwameg
kwameg
10,066 Points

NEED ASSISTANCE ACCESSING $_FILES FOR FILE UPLOAD FORM VALIDATION IN PHP

In the following input tag, <input type="file" name="doc">, how can I check a file was uploaded or the file field is not empty before a form submission?

I have tried using the following; isset($_FILES["doc"]), !empty($_FILES["doc"]), isset($_FILES["doc"]["name"]), !empty($_FILES["doc"]["name"]), isset($_FILES["doc"]["size"]), !empty($_FILES["doc"]["size"]) and so on, all to no avail. I can't access the file varible .

Hope I can get some assistance to sort this out

2 Answers

Paul Yabsley
Paul Yabsley
46,713 Points

You could check the size of the file.

if(($_FILES["doc"].["size"]) > 0) { ... }

http://www.php.net/manual/en/features.file-upload.post-method.php

kwameg
kwameg
10,066 Points

Thanks Paul but it's somehow still not working.

I have tried all the possible combinations of $_FILES["doc"]["size"] and $_FILES["doc"]["tmp_name"], all to no avail.

Paul Yabsley
Paul Yabsley
46,713 Points

I'm not sure then. I've used the above and it worked in my situation. Maybe post some more of your code or link to an example and myself or someone else might be able to help.