Skip to content

BUG: upload a file with the same name of one already present in the filemanager #938

@redwiz1

Description

@redwiz1

this piece of code will be executed

if(file_exists ($fullPath) && !$override_file_name && !$chunks) {
$ext_1 = $ext ? '.'.$ext : '';
$fullPath = $path . '/' . basename($fullPathInput, $ext_1) .'_'. date('ymdHis'). $ext_1;
}

and, if override_file_name is false , the following error will be raised:

PHP Notice: Undefined variable: chunks

The file is still generated properly, if the upload is in only 1 chunk.
The real problem is when the file is big and chunk upload is needed, every chunk will have a different name, the resault will be something like this:

myfile.gz 26.41 MB ---> original file
myfile_230111154901.gz.part 3.81 MB ---> chunk 1 with timestamp
myfile_230111154909.gz.part 3.81 MB ---> chunk 2 with timestamp
myfile_230111154913.gz.part 3.81 MB ---> chunk 3 with timestamp
myfile_230111154916.gz.part 3.81 MB ---> chunk 4 with timestamp
myfile_230111154919.gz.part 3.81 MB ---> chunk 5 with timestamp
myfile_230111154923.gz.part 3.81 MB ---> chunk 6 with timestamp
myfile_230111154926.gz 3.52 MB ---> last chunk with timestamp, renamed

This is a temporary fix, with lot less problems (it has problems if 2 users try to upload the same big file at the same time )
i deleted that entire conditional, and i replaced this:

if ($chunkIndex == $chunkTotal - 1) {
rename("{$fullPath}.part", $fullPath);
}

with this:

    if ($chunkIndex == $chunkTotal - 1) {
      if (file_exists($fullPath) && !$override_file_name) {
        $ext_1 = $ext ? '.' . $ext : '';
        $newFullPath = $path . '/' . basename($_REQUEST['fullpath'], $ext_1) . '_' . date('ymdHis') . $ext_1;
      } else {
        $newFullPath = $fullPath;
      }
      rename("{$fullPath}.part", $newFullPath);
    }

A proper solution would be using a unique filename generated with tempnam for the chunks ,saved in the session

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions