Check file exist on UploadiFive

247 Views Asked by At

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;
}
1

There are 1 best solutions below

6
Nithin Suresh On

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;
    }
    }