如何在OctoberCMS中将文件输入从HTML传递到PHP

Here is a demonstration of the issue:

HTML

<form data-request="onSubmit" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileUpload" id="fileUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

PHP

function onSubmit()
{
    echo $_FILES["fileUpload"]["name"];
}

This code always outputs

"Undefined index: fileUpload"

Which clearly means that PHP isn't able to access the data since the index that should contain a file is undefined. How would I be able to pass a file input by the client into PHP?

Solutions can use javascript with jQuery if necessary.

P.S. I already tried the solution here. It did not work.

It's a lot better idea to use the built in helper classes to access the post data. Use this in you handler function to access the uploaded file:

Input::file('fileUpload');

Uploading files through AJAX is fairly complicated, I recommend that you utilize the frontend file upload plugin (or check out the code it's based on and implement it yourself) to handle that.

See octobercms/october#2428, octobercms/october#2627, and https://octobercms.com/forum/post/frontend-file-upload for more information on this topic.

If you just changed data-request="onSubmit" to action="someURL.php" the form should work just fine.