skip to Main Content

I’m trying to upload files to the /logos directory in my S3 bucket, but whenever I upload it, two things happen:

  1. The file gets stored in the root directory
  2. 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


  1. Chosen as BEST ANSWER

    The answer to the question is the following:

    if($request->hasFile('logo')){
                $file = $request->file('logo');
                $extension = $file->getClientOriginalName();
                $fileName = time() . $extension;
                $path = "logos/".$fileName; // we specify the path here
                $t = Storage::disk('s3')->put($path, file_get_contents($file)); // we use "file_get_contents" here to upload the file.
                if (!$t) {
                    dd('Failed to upload logo file.');
                }
    
                $validated['logo'] = $path;
            }
    

  2. To resolve this issue chage your code to be like this:

    // Store the file in the "uploads" directory on S3

    $uploadedFile = $request->file('file');
    Storage::disk('s3')->put('uploads/' . $uploadedFile->getClientOriginalName(), file_get_contents($uploadedFile));
    
    Login or Signup to reply.
  3. 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 :

    $validated = $request->validate([
        'logo' => ['required', 'file', 'mimes:jpeg,png', 'max:2048'],
    ]);
    
    if ($request->hasFile('logo')) {
        $file = $request->file('logo');
        $extension = $file->getClientOriginalExtension(); // Get file extension
        $fileName = time() . '.' . $extension; // Create a unique filename
        $path = 'logos/' . $fileName; // Specify the desired directory in S3
    
        // Use the putFileAs method to store the file in the specified directory
        if (Storage::disk('s3')->putFileAs('', $file, $path, 'public')) {
        // File uploaded successfully
        $validated['logo'] = $path;
        } else {
        dd('Failed to upload logo file.');
        }
    }
    
    // Rest of your code to save the validated data
    
    • First, we ensure that the uploaded file meets the requirements, like
      being of the right type (JPEG or PNG) and not exceeding 2MB in size.
    • If a valid file is uploaded, we prepare to store it in the /logos
      directory within your S3 bucket.
    • To avoid overwriting files and ensure uniqueness, we create a new
      filename by combining the current time and the original file’s
      extension.
    • We specify the path where the file should be stored, which is /logos/
      plus the unique filename.
    • Using the putFileAs method, we upload the file to S3 with the
      specified path and filename. We also mark the file as ‘public’ to
      make it accessible.
    • If the upload is successful, we update the validated data with the
      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.

    Login or Signup to reply.
  4. Modify your code according to below code. This will upload your image to logos directory inside s3 disk. I used storeAs() instead of using put()

    if ($request->hasFile('logo')) {
            $file = $request->file('logo');
            $extension = $file->getClientOriginalName();
            $fileName = time() . $extension;
            $path = $file->storeAs('logos', $fileName, 's3');
            if (!$path) {
                dd('Failed to upload logo file.');
            }
    
            $validated['logo'] = $path;
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search