hamedgasemi200's avatar

The file was not uploaded due to an unknown error.

  1. I've uploaded / saved a file at: /var/www/laravel/storage/downloads/[FILE_NAME.EXT]
  2. I make an object of Illuminate\Http\UploadedFile like this: $file = new UploadedFile("/var/www...FILE.EXT", "FILE.EXT");
  3. Then I try to move the uploaded / saved file.
# Move File
$file->move($data['base_path'], "{$data['full_path']}");
  1. It returns: [FILE_NAME] was not uploaded due to an unknown error.

This code works correctly with other things. But in this one, it doesn't.

0 likes
6 replies
mstrauss's avatar

Can you dd $data? It's hard to figure out where the error is without it. As it could be in the move method if improper arguments are being passed.

hamedgasemi200's avatar

Sure, but it's not that much important. anyways, there you go:

$data['full_path'] = /var/www/laravel/storage/uploads/2019/07/22/FILE.EXT
$data['base_path'] = /var/www/laravel/storage/uploads/2019/07/22/
mstrauss's avatar

Hmm.. Can you provide the stack trace on the error?

hamedgasemi200's avatar
Symfony\Component\HttpFoundation\File\Exception\FileException: The file "dyu3fosyl7mDpMIAoyAJ" was not uploaded due to an unknown error. in /var/www/laravel/vendor/symfony/http-foundation/File/UploadedFile.php:236
Stack trace:
#0 /var/www/laravel/app/Http/Controllers/WEB/Files.php(130): Symfony\Component\HttpFoundation\File\UploadedFile->move('/var/www/larave...', '/var/www/larave...')
#1 /var/www/laravel/app/Jobs/Download.php(84): App\Http\Controllers\WEB\Files->saveFile(Object(Illuminate\Http\UploadedFile))
#2 [internal function]: App\Jobs\Download->handle()
#3 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
..................
mstrauss's avatar

Looks like a file extension error per the Symfony framework:

            case UPLOAD_ERR_EXTENSION:
                throw new ExtensionFileException($this->getErrorMessage());

But, the output error message is being transformed to a generic one due to $errors[$errorCode} not being set. Do you have your app env set correctly (assuming you are in local dev with debug set to true).

$message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';

The actual error message should say: "File upload was stopped by a PHP extension".

For more info on that, check this out from the docs: https://php.net/manual/en/features.file-upload.errors.php

UPLOAD_ERR_EXTENSION Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.

hamedgasemi200's avatar

I think you're right. But I solved it by using mkdir and rename functions instead of move.

Please or to participate in this conversation.