-1

I'm novice and i want save images from a Form in php.

my code is pretty simple:

<form method="POST" enctype="multipart/form-data" action="">
                
    <label for="nom">Nom :</label><input type="text" name="nom" id="nom">
    
    <label for="adresse">Adresse :</label><input type="text" name="adresse" id="adresse">
    
    <label for="tel">Numéro de télephone :</label><input type="text" name="tel" id="tel">
    
    <label for="mail">E-mail :</label><input type="text" name="mail" id="mail">
    
    <label for="web">Url du site web</label><input type="text" name="web" id="web">
    
    <label for="images">Logo/image</label>
    <input type="file" multiple name="images" id="images" />
    
    
    <input type="submit" class="bouton" id="valider" value="valider"/>
</form>

and I just want see if my images arrive in my $_FILES.

so i have at the top of my index.php

<?php 

echo var_dump($_FILES);

?>

when i press the submit button the 1st time, it's still empty, but if i press it a second time, i get it.

do someone knows why ?

thank you.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • can you post it to another page like: action="another-page", currently action is empty as you can see. Also, you do not need echo with var_dump, just var_dump should be sufficient – gguney Feb 21 '22 at 10:45
  • @gguney, if the same webpage is handling a post request too, you can put action="" empty in your
    tags.
    – BendaThierry.com Feb 21 '22 at 10:47
  • yes because my echo is at the top of the same page, but when i do it with an another page, the array is empty – Anthony Brochier Feb 21 '22 at 10:47
  • @BendaThierry.com yeah I know just to something – gguney Feb 21 '22 at 10:50
  • 1
    First of all, `echo var_dump()` is pretty nonsensical. var_dump writes to the output buffer directly, and does not have any return value. – CBroe Feb 21 '22 at 10:53
  • 1
    Second, it makes no sense why you would get a different result, when you submitted this form a second time. I am guessing the first request you are talking about is not actually _submitting_ the form, but only the one that _shows_ the from initially? – CBroe Feb 21 '22 at 10:54
  • Because you are processing the form inputs in the same page as you use to present the form, the first time the page runs it will attempt to process the form inputs but of course they will not be there as the form has not yet been populated and submitted. Its normal to check in some way to see if the form has been submitted or this is an initial page load. See the duplicate for examples of how to do this – RiggsFolly Feb 21 '22 at 10:59

1 Answers1

0
  • The first click is putting you on the webpage of the form as it can't work without fields populated.
  • The second click is running your action with files inside it, so it works.

Why ? Because you are displaying your form in various webpages, you must handle your form action in a website global point of view. You are using php so try to use the layout feature for html is one thing, but do not forget to do the same for handling your POST action globally.

BendaThierry.com
  • 2,080
  • 1
  • 15
  • 17