0

I created a form here with Laravel and DropzoneJS and I tried uploading a Gimp file (.xcf) and when it is uploaded it is saved in S3 as the following

<random-name>.

without the "xcf" extension just random name ending with a dot.

Also, I created a text file and renamed it to test.xcf when I tried uploading that file it was uploaded with the .txt extension.

Here is my UploadController.php which handles the upload:

<?php

namespace App\Http\Controllers;

use App\Upload;
use Illuminate\Http\Request;

class UploadController extends Controller
{
    public function upload(Request $request)
    {
        $originalName = $request->file('file')->getClientOriginalName();
        $fileSize = $request->file('file')->getClientSize();
        $path = $request->file('file')->store('documents');
        $explode = explode('documents/', $path);
        $name = $explode[1];
        $uniqueId = $this->generateUniqueId();

        $upload = new Upload();
        $upload->unique_id = $uniqueId;
        $upload->name = $name;
        $upload->path = $path;
        $upload->original_name = $originalName;
        $upload->size = $fileSize;

        if ($upload->save())
        {
            return response()->json([
                'original_name' => $originalName,
                'size' => $fileSize,
                'url' => env('AWS_URL') . $path,
                'id' => $uniqueId,
                'status' => 'OK'
            ]);
        }

        return response()->json(['status' => 'BAD', 'message' => 'There was a problem saving your file.']);
    }

    public function generateUniqueId()
    {
        $result = '1';
        $result .= rand(100000000, 999999999);

        while(Upload::where('unique_id', '=', $result)->first())
        {
            $result = '1';
            $result .= rand(100000000, 999999999);
        }

        return $result;
    }
}

I've got no idea why it's doing that.

2 Answers 2

1

I suggest, you generate your own hash for filename, like I do in this code: $file = $request->file('csv'); $path = $file->storeAs( 'csv', md5($file->getClientOriginalName()) . $file->getClientOriginalExtension(), 's3' );

You can also add uniqid() to md5 input

Sign up to request clarification or add additional context in comments.

Comments

0

If you're using laravel 5+ then you should get the extension also using this.

$extension = $file->getClientOriginalExtension();

This will work fine.

3 Comments

The problem is not getting the extension but rather that the uploaded file's new name ends with a dot "." or it gets renamed to ".txt"
You have to append the extension in it.
Append it where, Laravel automatically generates a file name with extension and it stores the file in S3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.