Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.