1

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.

1 Answer 1

1

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();

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

10 Comments

I get "Call to a member function extension() on null". Maybe Input::file() isn't actually working?
Check my updates. I hope this works. Please rename your input to 'name', just to avoid cconfusion with Laravel file function.
I'm not knowledgeable enough with laravel to know where to put this or use the controller. In OctoberCMS I have my uploader in a custom Component.
I think it would still work. The function that handles your file upload, just pass Request $request as a parameter and do the rest inside of the function. I think it should work, though Im not familiar with October CMS
ok check my update, I found out how October CMS does it. No need to pass request anymore. Just the way you were doing it already at the begining, only with a different function.
|

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.