954,148 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Conditional if statement for <input type="file">

I have a form that has an option to upload an image along with some text and a title. What I want to do is check if the input for uploading the image is empty. If it is then the page processes the code just to upload the text portion to the database. If it isn't empty then it processes the code that uploads the image as well as the text.

I used

if (!$_POST['image']) {


which at first I thought did the trick but when I tried uploading an image it didn't work. I know for a fact that the code for uploading an image works as I've used it on another page to test it. So I'm left to believe that the conditional statement is the source of the problem.

Is there another way of checking if someone has browsed and chosen an image file for uploading?

Venom Rush
Posting Whiz
350 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 
<?php
if(isset($_POST['submit'])) {
		if($_FILES['fileupload']['name']==""){
			echo "Empty"; //no file was uploaded
		}
		//continue with rest of the operation..
}
	?>
<html>
<body>
<form name="upload" method="post" action="upload.php" enctype="multipart/form-data">
<table>
<tr><td>
<input type="file" name="fileupload">
</td></tr>
<tr><td>
<input type="submit" name="submit">
</td></tr>
</table>
</form>
</body>
</html>

This will do.. :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

I could kick myself right now. Thanks once again nav33n. May I one day be as incredibly smart as you are ;)

Venom Rush
Posting Whiz
350 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

You are welcome! :D

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Just for those that would like to know what I wanted the conditional statement for, here's an example:

if ($_REQUEST['submit']) {
        
        //checks if there is no image selected for uploading
	if ($_FILES['image']['name'] == '' ) {

        //the code for inserting the text into the database

        } else {
        
        //the code for uploading the image and the text.

        }
Venom Rush
Posting Whiz
350 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You