Why does Undefined index mark me in PHP Using $ _FILES for Images?

0

Well my problem is that I'm trying to store images in my database in mysql this using the php language and you show me this notification

   Notice: Undefined index: file-1[] in C:\xampp\htdocs\Hidtruth\PHP\configuracion\save.php on line 8

Here is the code of my file-type input.

<input type="file" name="file-1[]" id="file-1" class="inputfile inputfile-1" data-multiple-caption="{count} files selected" multiple />

Here is the code of my capture in php of my supposed file

$imagen=$_FILES["file-1[]"]["temp_name"];
echo $imagen;

If you realize it you can see that I do an echo towards the image to see what I get but I get the error above. What I want is to give me the path of my file and then copy it to a folder in my project.

    
asked by David 17.08.2016 в 20:16
source

3 answers

1

There are several errors which are causing the problem you indicate (and others that still do not appear).

If in HTML you define an input to upload multiple images / files (as in your case with file-1 []), from PHP no you should put [] in such a way that it looks like this:

$imagen=$_FILES["file-1"]["temp_name"];

Another possible error you may have is that you are not sending the enctype property in the form. You should see the statement you have of the form like this:

<form method="POST" enctype="multipart/form-data">

Finally, there is no "temp_name", what exists in PHP is "tmp_name". Fix that error too:

$imagen=$_FILES["file-1"]["tmp_name"];

Here you can see a small article describing how to upload images with PHP and a web form:

link

    
answered by 17.09.2016 в 18:04
0

How about, the way you get the variable, either by POST or by GET, you can try this: By POST

$variable = isset($_POST['file-1[]'])? $_POST['file-1[]']:'';

By GET

$variable = isset($_GET['file-1[]'])? $_GET['file-1[]']:'';
    
answered by 17.08.2016 в 21:35
0

When you define the name of the input in the HTML, you give it an array value:

<input type="file" name="file-1[]" id="file-1" class="inputfile inputfile-1" data-multiple-caption="{count} files selected" multiple />

So when it is sent by the form, it will be done in the form of an array and this is how you will receive it in the PHP POST.

The index of the array $_FILES when you receive it will be file-1 but the content will be an array, so for the first file received, you will have to indicate the sub-index in the following way:

$imagen=$_FILES["file-1"][0]["tmp_name"];

To give you a better idea, you can start by printing the contents of the variable $_FILES to see what you're getting too, that always helps:

print_r($_FILES);

I also recommend that you read the response of Arturo Belanio Lima that provides related information that may be useful.

    
answered by 01.01.2017 в 18:12