Page 1 of 1

Uppy "Upload failed" message

Posted: Tue Feb 18, 2025 8:36 am
by vario
Since updating to the latest version Uppy is throwing an error where it was OK previously.

I have used the example at uppy.io to guide me.

I am calling a PHP procedure using uppy.setMeta, uploading a CSV and processing it before loading into the database. The procedure works OK, but Uppy reports an upload failure with an alert message containing "undefined" shown twice.

In the server logs I have this:

[Tue Feb 18 06:49:48.726713 2025] [php:warn] [pid 73461:tid 73461] [client 192.168.1.34:41874] PHP Warning: http_response_code(): Cannot set response code - headers already sent (output started at /var/www/forte/core/nucommon.php(1473) : eval()'d code:69) in /var/www/forte/core/nucommon.php(1473) : eval()'d code on line 235, referer: https://myhhost/forte/index.php

If I comment out the calls to http_response_code in my PHP this error goes away, but the Uppy example uses this so why the PHP Warning?

I am having trouble tracking the problem down - I have tried console.log(result) in a nuOnFileUploadComplete() but can't make sense of that. I have other similar procedures that still work.

Any help would be appreciated though I appreciate there's not much here to go on...

Database: Arch Linux 11.7.2-MariaDB
PHP: 8.4.3
nuBuilder DB: V.4.5-2025.02.13.01
nuBuilder Files: V.4.5-2025.02.12.00

Neil.

Re: Uppy "Upload failed" message

Posted: Tue Feb 18, 2025 9:36 am
by kev1n
Hi,

I've just tested it with the latest on Github using my custom upload procedure and it works fine.

Code: Select all

// Allowed file extensions
$allowed = array('xlsx');

// Maximum file size
$maxfilesize = 2 * 1024 * 1024; // (2 MB)

$filename = nuSanitizeFilename(basename($_FILES['file']['name']));
$target_dir = $_SERVER['DOCUMENT_ROOT']. "/ressources/upload/";

$error = nuTranslate('Sorry, there was an error uploading your file.');

$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) {
    $data = ['error' => $error, 'message' => 'Invalid File Type'];
    $result = json_encode($data);
    return;
}

$target_file = $target_dir . $filename;
$filesize = $_FILES["file"]["size"];

if ($filesize > $maxfilesize) {
    $data = ['error' => $error,
        'message' => 'File size exceeded'];
    $result = json_encode($data);
}

try {
    if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
        $data = ['url' => $target_file,
            'file' => $file,
            'message' => 'The file ' . $file . ' has been uploaded.'];
        http_response_code(201);
        $result = json_encode($data);
    } else {
        throw new Exception('Unable to move the uploaded file to its final location:' . $target_file);
    }

} catch (Throwable $th) {
    $data = ['message' => $error,
        'error' => $th->getMessage()];
    $result = json_encode($data);
}



Re: Uppy "Upload failed" message

Posted: Tue Feb 18, 2025 9:53 am
by vario
Yes, I have other uploads working via the same method - just this one that has started to return a failure.

The puzzle is that the file uploads correctly to the server i.e it is saved at the specified location and complete. All the required processing is successful and I have used PHP error_log to confirm the procedure is trying to set http_response_code(201) indicating a successful outcome. Only then do I get the failure returned by Uppy.

I'll just have to keep on digging around I guess.

Re: Uppy "Upload failed" message

Posted: Tue Feb 18, 2025 12:10 pm
by kev1n
To reproduce the issue, we’ll likely need more details. Could you share your uppy JS code and the corresponding PHP procedure code? Without this information, recreating the problem would be quite time-consuming, and we'd only be guessing how your implementation looks.

Re: Uppy "Upload failed" message

Posted: Tue Feb 18, 2025 1:30 pm
by vario
It turns out a deprecated PHP code feature was causing some output before the call to http_response_code(201).

I changed str_getcsv($line) to str_getcsv($line,',','"','') and that fixed it.