Log in

View Full Version : How to check whether the input is file



probyte2u
09-16-2012, 04:58 AM
My script is like this :

if (got image) { show the image }
else { show the input file for user to upload the image }


In the php script how to validate when user submit the form via post , the post got $_FILES information or not ?

Thank in advance for your kind assistance . Tx

djr33
09-16-2012, 05:22 AM
This is the sort of general question that is best answered by a tutorial or the php.net website.

This part of the php.net website is designed to explain this clearly:
http://php.net/manual/en/features.file-upload.php

Unfortunately, that page is actually fairly technical and might be difficult if you're just starting out. But it's good information and I do suggest looking at it. A good way to start might be to copy some of the example code and play with it. Just be careful of course, and if you're not sure what you're doing, then I suggest working on a server only after you've backed up all of your files. Also, while you're learning this (and might not understand the security risks of file uploads) don't make this a public page-- don't share the URL with anyone. After you understand how it works, you can make it a public site.


If that doesn't work for you (and it's not necessarily the best/only way to start), you should just use google to search for a "php file upload tutorial", and that should work.

If you have any more specific questions, feel free to ask.


In the php script how to validate when user submit the form via post , the post got $_FILES information or not ?See this page for detailed information: http://www.php.net/manual/en/features.file-upload.post-method.php

You can use something like the "validating file uploads" (Example #2) section on the page.

Or if you simply want to know that a file was submitted, check that $_FILES['userfile']['name'] was submitted:
if (isset($_FILES['userfile']['name'])) {

probyte2u
09-16-2012, 07:54 AM
Thanks a lot for the tip, will use the isset method to check for the $_FILES submission. Thanks a lot...