I’m trying to upload files to the /logos
directory in my S3 bucket, but whenever I upload it, two things happen:
- The file gets stored in the root directory
- The file is corrupted whenever I try to download it directly from the AWS console, meaning that the file is corrupted anyways
This is how my code currently looks like:
$validated = $request->validate([
'logo' => ['required', 'file', 'mimes:jpeg,png', 'max:2048'],
]);
...
if($request->hasFile('logo')){
$file = $request->file('logo');
$extension = $file->getClientOriginalName();
$fileName = time() . $extension;
$path = Storage::disk('s3')->put($fileName, 'logos/');
if (!$path) {
dd('Failed to upload logo file.');
}
$validated['logo'] = $path;
}
...
How can I correctly upload files and store them in the desired directory?
4
Answers
The answer to the question is the following:
To resolve this issue chage your code to be like this:
// Store the file in the "uploads" directory on S3
It seems like you want to upload a file to a specific directory in your S3 bucket, but your current code is not achieving that. To upload files to the /logos directory in your S3 bucket and ensure they are not corrupted, you need to make a few adjustments to your code. Here’s a corrected version of your code :
being of the right type (JPEG or PNG) and not exceeding 2MB in size.
directory within your S3 bucket.
filename by combining the current time and the original file’s
extension.
plus the unique filename.
specified path and filename. We also mark the file as ‘public’ to
make it accessible.
path to the uploaded file. If it fails, we display an error message.
These changes will help you upload files to the /logos directory in your S3 bucket and ensure they are correctly stored and accessible without corruption.
Modify your code according to below code. This will upload your image to logos directory inside s3 disk. I used
storeAs()
instead of usingput()