6

OK, i have some kind of strange problem with my code. I'm trying to make uploading form with text along photos. Now, I need to check if the photo is uploaded or the text is inserted only.

The simplified code looks like this:

if (isset ($_FILES['image'])){
echo 'yes';


} else {    
echo 'no';      
}

the form looks like:

   <form action=""  method="post" enctype="multipart/form-data" class="contact-form">
   <form action=""  method="post" enctype="multipart/form-data" class="contact-form">


   <label for="title"><strong>Title</strong> (required)</label>
   <input type="text" name="title" value="" required>


   <label><span class="small"><strong>Add a photo</strong></span></label>
   <input type="file" name="image"/>


   <input type="submit" name"submit" value="Submit">    
   </form>

Now, basically, what I'm trying to make is a check if file is attached or not, because I need an alternate option for posting posts without photos, in the same form; but, the problem is that no mather if it is attached or not it always says YES.

Help, anyone.

3
  • Try if (!empty($_FILES['image'])) { ...? Commented Jul 5, 2013 at 15:17
  • Nop, same thing happens. I insert the title only, it says the file has been inserted. Commented Jul 5, 2013 at 15:19
  • 1
    why it is 2 times <form> ?? Commented Jul 5, 2013 at 15:25

5 Answers 5

20

To check if the file has been uploaded, how about:

if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) 
{
    echo 'No upload';
}
else
{
    // Your file has been uploaded
}

is_uploaded_file() is the choice here, check the docs out for it.

$_FILES is an array of files, and each file in the first dimension of the array is automatically given a ['tmp_name'] key in the second dimension, with the value being it's temporary location.

Obviously you then need to use move_uploaded_file() on the temporary file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I have solved it with this. P.s. uploading part isn't the problem (moving, entering db, everything works fine, this part was, when there is no file, just text) Thanks.
4

You are checking only that $_FILES['image'] passed from your form.
But you are not checking that posted $_FILES['image'] is empty or its contains a valid file data
Read more here http://www.php.net/manual/en/features.file-upload.php

<?php
  if ( isset( $_FILES["image"] ) && !empty( $_FILES["image"]["name"] ) ) {
    if ( is_uploaded_file( $_FILES["image"]["tmp_name"] ) && $_FILES["image"]["error"] === 0 ) {
      // everything okay, do process
      echo 'yes';
      exit();
    }
  }
  echo 'no';
?>

Comments

2

The reason "isset" doesn't work is most likely because $_FILES is considered set even when it is set to "null". I found the empty() suggestion to be the cleanest. Although, unless you're using a large form with multiple files, I would remove the brackets entirely.

if (empty ($_FILES)){
 echo 'No file';
 } else {    
 echo 'File found';
 }

Comments

0

Use empty() instead.

if (!empty ($_FILES['image'])){
    echo 'yes';


} else {    
    echo 'no';      
}

1 Comment

doesn't work, stil echoes 'yes' when the title is entered. I need 'no' :)
0
if(empty($_FILES['image']['name'])){echo 'not attached successfully';}
else{echo 'attached successfully';}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.