0

I downloaded this upload example: https://github.com/xmissra/UploadiFive

When sending an apernas file, it works perfectly but when sending more than one file the function that checks if the file already exists presents a problem. Apparently the loop does not ignore that the file has just been sent and presents the message asking to replace the file. Is there any way to solve this?

This is the function that checks if the file already exists.

$targetFolder = 'uploads'; // Relative to the root and should match the upload folder in the uploader 
script

if (file_exists($targetFolder . '/' . $_POST['filename'])) {
    echo 1;
} else {
    echo 0;
}

You can test the upload working at: http://inside.epizy.com/index.php

*Submit a file and then send more than one to test.

I tried it this way but it didn't work:

$targetFolder = 'uploads';

$files = array($_POST['filename']);

foreach($files as $file){
if (file_exists($targetFolder . '/' . $file)) {
    echo 1;
} else {
    echo 0;
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ed Dias
  • 175
  • 1
  • 3
  • 11
  • see https://stackoverflow.com/questions/20597285/using-uploadifive-for-multiple-file-upload-causes-repeated-invocations-of-check – George Birbilis Jun 14 '21 at 04:35

1 Answers1

0

When you have multiple files to be uploaded keep these things in mind;

HTML

  1. Input name must be array name="file[]"
  2. Include multiple keyword inside input tag.

eg; <input name="files[]" type="file" multiple="multiple" />

PHP

  1. Use syntax $_FILES['inputName']['parameter'][index]
  2. Look for empty files using array_filter()

    $filesCount = count($_FILES['upload']['name']);
    
    // Loop through each file
    for ($i = 0; $i < $filesCount; $i++) {
    
     // $files = array_filter($_FILES['upload']['name']); use if required 
    
    if (file_exists($targetFolder . '/' . $_FILES['upload']['name'][$i])) {
        echo 1;
    } else {
    echo 0;
    }
    }
    
Nithin Suresh
  • 360
  • 1
  • 7
  • I tried it this way but it didn't work: `$targetFolder = 'uploads'; $files = array($_POST['filename']); foreach($files as $file){ if (file_exists($targetFolder . '/' . $file)) { echo 1; } else { echo 0; }` – Ed Dias Feb 10 '20 at 21:08
  • Have you enabled multiple files in the input tag for your html . https://www.w3schools.com/tags/att_input_multiple.asp – Nithin Suresh Feb 10 '20 at 21:12
  • If you have used multiple in the input tag. You have to use $_FILES instead of $_POST for getting the file name. – Nithin Suresh Feb 10 '20 at 21:27
  • It also didn't work this way `$files = array($_FILES['filename']);` – Ed Dias Feb 10 '20 at 21:37
  • Its not the right way to use $_FILES : https://www.php.net/manual/en/features.file-upload.php – Nithin Suresh Feb 10 '20 at 21:40
  • Hi Nithin Suresh, his example uploads but now he doesn't check if the file already exists. When sending a file with the same name, the script does not ask if you want to replace i – Ed Dias Feb 11 '20 at 18:20