Matt McManis
Matt McManis

Reputation: 4675

Dropzone.js "Undefined index: file" How to use with Laravel?

I'm using OctoberCMS, based on Laravel.

I have a working HTML form for uploading files.

<form class="dropzone" method="POST" action="/upload.php" enctype="multipart/form-data">

    <input type="hidden" name="_handler" value="onUpload" />

    <div class="fallback">
        <input type="file" name="file">
    </div>

    <input type="submit" value="Upload" />

</form>

Dropzone

I'm trying to add Dropzone.js to it. It says you just add the class to the form.

From Dropzone docs:
http://www.dropzonejs.com/#usage

The uploaded files can be handled just as if there would have been a html input like this:

<input type="file" name="file" />

That's what my form type and name already was before adding Dropzone.

Error

But when it gets to this line in my upload.php, I get an error:

$inputName = basename($_FILES['file']['name']);
$inputExtension = pathinfo($inputName, PATHINFO_EXTENSION);

Error: Undefined index: file

But it worked before without Dropzone, using the same name 'file'.

Laravel

It will pass without error if using:

$inputName = Input::file('file');

But now I have difficulty getting the file extention, because it's no longer in the variable using Input::file.

Upvotes: 1

Views: 1117

Answers (1)

EddyTheDove
EddyTheDove

Reputation: 13259

You can do in your controller

$inputExtension = request('file')->extension();
$path = request('file')->path();
$file = $request->file('file');

Update

Please pass the request to your controller and rename your input to avoid confustion with Laravel 'file'.

public function store(Request $request)
{
    $file = $request->file('image');
    $extension = $request->image->extension(); //or
    $originalExtension = $file->getClientOriginalExtension();

    $path = $request->image->path();
}

How october CMS does it

$extension = Input::file('file')->getClientOriginalExtension();
$name = Input::file('file')->getClientOriginalName();

So the key was to use getClientOriginalExtension();

Upvotes: 1

Related Questions