How to write a post from an array in the current index - input file

4

Next, 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 completed do the update. I have the partial solution that passed me which 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 loop code today looks like this:

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 some way to check if the input is empty, if I am playing the value in the $ numFile then I do the Update or not, the rest of the program is working, already with images in bank and everything. The problem is that without conditional, regardless of whether I already have an image uploaded, if I click upload without uploading photo it will overwrite my bank with whitespace

    
asked by anonymous 26.03.2014 / 22:01

3 answers

1

Test like this:

$file= $_FILES['file1'];   // trate este valor como uma array
foreach( $file as $esteFile ) {
  if( $esteFile != '') {
     // UPDATE
    }
}
    
26.03.2014 / 22:07
0

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

if (isset($_FILES['file1']) && $_FILES['file1'] != ''){
   //update
}
    
26.03.2014 / 22:05
0

I think that because it is an array you should validate it as such.

note that your input is like this


    ""

The ideal would already be to be with the numbers defined, type:


     
     
     


// 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 this to be

    
01.04.2014 / 21:59