-1

Ok, I'm confused. I am trying to learn how to upload an image file in PHP.

<form name="add_form" action="submit.php" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<button type="submit">Submit</button>
</form>

in the submit.php file:

print_r($_FILES);
    if(empty($_FILES['file']['name'])){
        echo "empty";
    }else{
        echo "not empty";
    }

print_r($_FILES); shows me this:

Array ( [image] => Array ( [name] => van Gogh - GalleryPlayer.jpg [type] => image/jpeg [tmp_name] => /tmp/phpbYjG7m [error] => 0 [size] => 1437795 ) ) 

But when I run if/else using (empty($_FILES['file']['name'])) it always echos "empty". How can it be empty if it is in the print_r($_FILES) array?

My ultimate goal here is to check if the user has uploaded an image file (it's part of a larger form). If they do, use it. If not, use a default image. So i'm just trying to test for the existence of the form field. Make sense?

Thanks for any insight you may be able to provide.

Cheers!

midasxl
  • 35
  • 5
  • You need to use the `name` attribute to access the file `if($_FILES['image']['name'] == "") echo "empty";` – Amr Aly Sep 02 '18 at 01:31
  • Your `print_r()` output suggests you should use `empty($_FILES['image']['name'])` rather than `['file']['name']` – Michael Berkowski Sep 02 '18 at 01:31
  • Tried this: `if(empty($_FILES['image']['type']))` and also `if($_FILES['image']['name;] == "")`. Both fall into the first condition "empty". Seems like all the variations I try, they all come back empty. – midasxl Sep 02 '18 at 01:52
  • Just tried this too: `if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name']))`. Still comes back empty. – midasxl Sep 02 '18 at 02:13

1 Answers1

-1

$_FILES['image']['name'] is ok. try again.Then similar access to other fields