How to write an array post in current Intel - input file

Asked

Viewed 192 times

4

Next guys, I’m adapting a file upload system to php. All the bulk has already been created, Insert and update and such, but in the middle of the logic needing a conditional to check if the input file field was used, if it is empty do nothing, if it is filled do the update. I have the partial solution they gave me that was this:

if ($_POST['file'] != '') {
 UPDATE
}

But I’m using php and the upload field comes from an array as follows

<input type="file" id="file1" name="file1[]"></input> 

How do I write this if considering the current loop ?

My bow tie code is like this today:

if($numFile <= 0){ //Laço que vai servir pra checar se o input está vazio
    echo 'Selecione uma Imagem!';
}else{
for($i = 0; $i < $numFile; $i++){
    $name   = $file['name'][$i];
    $type   = $file['type'][$i];
    $size   = $file['size'][$i];
    $error  = $file['error'][$i];
    $tmp    = $file['tmp_name'][$i];  .... e o programa segue

I needed to somehow check if the input is empty, if the value is set in the $numFile then I do the Update or not, all the rest of the program is working, already with bank images and everything. The problem is that without it is conditional, independent of already having an upada image, if I click upload without submitting a photo it will overwrite my bank with white space

3 answers

1

Test like this:

$file= $_FILES['file1'];   // trate este valor como uma array
foreach( $file as $esteFile ) {
  if( $esteFile != '') {
     // UPDATE
    }
}
  • Hmm, Testing...

  • Test but it did not work, to simplify I used like this: <pre> $file2= $_FILES['file1']; // treat this value as an array foreach( $file2 as $esteFile ) { if( $esteFile != ') { $numFile = 0; } } </pre> if($numFile <= 0){ echo 'Select an Image! '; }Else{

  • Sorry for the formatting, I’m still learning to use the stackoverflow, I’m a first-time sailor.

0

I believe that because it is a Voce array I should validate as such.

note that your input is so


    ""

the idea would be that he would be with the numbers set, like:


     
     
     


// entao se testaria mais ou menos assim...

for($i=1;$total;$i++){

   if( $file['error'][$i] == 0){
     echo 'este veio com erro!';
   } else{

    // continua o upload

   }
}

I believe that’s it

0

To check if there is any upload(file), you use $_FILES and not $_POST.

if (isset($_FILES['file1']) && $_FILES['file1'] != ''){
   //update
}
  • Testing ... thank you

  • 1

    What’s the problem with the answer @downvoter?

  • 1

    What about me? you @lost my tagged in the comment

  • @downvoter, which comment? on which question?

  • 1

    Now on that answer... you’ve put @downvoter that’s me

  • You’re right to use $_FILES +1.

  • 1

    @lost when you said "What’s the problem with the @downvoter answer"

  • It seems it was a mistake, @downvoter, your nickname before was not userAlgumNumer?

  • No @lost , always was downvoter! but blz was a misunderstanding.

  • @lost or better was a deception

  • It did not work, I will continue tomorrow in the war with PHP. Thank you for the force

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.