Reputation: 2062
When i try to upload a csv
file its MIME
type changes to application/octet-stream
. Although i specified enctype="multipart/form-data"
in the form, how can i fix that?
<div class="container mt-5">
<form action="{{route('fileUpload')}}" method="POST" enctype="multipart/form-data">
<h3 class="text-center mb-5">Download file</h3>
@csrf
@if ($message = Session::get('success'))
<div class="alert alert-success">
<strong>{{ $message }}</strong>
</div>
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="custom-file">
<input type="file" name="file" class="custom-file-input" id="chooseFile">
<label class="custom-file-label" for="chooseFile">Choose file</label>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-block mt-4">
Upload
</button>
</form>
</div>
Upvotes: 1
Views: 2507
Reputation: 7
You're experiencing some issues with file validation in Laravel. Specifically, when trying to retrieve the MIME type of a file using $file->getMimeType(), you're consistently getting application/octet-stream, while $file->getClientMimeType() provides the correct MIME type like application/pdf.
To resolve this, you're suggesting a custom validation approach. Instead of directly using Laravel's validation rules, you're proposing to define a custom validation rule. The custom rule would look something like this:
'file' => ['required', 'file', new CustomFileType('.pdf,.jpg,.png.doc')],
This custom validation rule (CustomFileType) would ensure that the uploaded file matches one of the specified file types (in this case, .pdf, .jpg, .png, or .doc).
Furthermore, to avoid the issue with MIME type retrieval, you recommend using $file->getClientMimeType() instead of $file->getMimeType() within your code.
By implementing these changes, you anticipate that the file validation issue will be resolved, and errors related to incorrect MIME types will no longer occur.
<?php
namespace App\Rules;
use App\Traits\GenericQueriesAllLobs;
use Illuminate\Contracts\Validation\Rule;
class CustomFileType implements Rule
{
use GenericQueriesAllLobs;
private $acceptedFile;
public function __construct($acceptedFile)
{
// $this->acceptedFile = $acceptedFile;
$this->acceptedFile = '.pdf,.jpg,.png;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$acceptedFileTypes = explode(',', $this->acceptedFile);
$extension = strtolower($value->getClientOriginalExtension());
return in_array('.'.$extension, $acceptedFileTypes);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The file must be a file of type: '.$this->acceptedFile;
}
}
Upvotes: -2
Reputation: 323
Recently I faced same problem - I'have trying to upload *.docx file to server using this code (on client):
function uploadFile(file) {
const formData = new FormData();
formData.append('file', file);
const config = {
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
}
}
return axios.post('/file/upload', formData, config);
}
server part:
// part from FileController.php
private const ALLOWED_TYPES = [
'image/png',
'image/jpeg',
'image/gif',
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
];
// FileUploader.php
class FileUploader {
public static function uploadFile($file, $maxSize, $uploadPath, array $allowedTypes = []): string
{
if( !in_array($file->getMimeType(), $allowedTypes) ) {
throw new \Exception("File type ({$file->getMimeType()}) not allowed." );
}
if( $file->getSize() > $maxSize ) {
throw new \Exception('File too big.');
}
$name = $file->getClientOriginalName();
$file->move( storage_path($uploadPath), $name );
return $name;
}
}
For most *.docx files uploading was successfull - it worked as expectet, but for certain sample.docx file - File type (application/octet-stream) not allowed. returned. And this is strange - only this file can't be uploaded.
The link that presented in previous comments, explain that this happens when server can't deterime file mime-type. So I fixed it by creating a new file with content of previous file - now it works.
Upvotes: 1