0

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.

Community
  • 1
  • 1
Mark Kramer
  • 3,134
  • 7
  • 34
  • 52
  • looks like something very specific to your CMS –  Feb 19 '17 at 22:22
  • No `action` attribute appears at `
    ` element. Where is `form` submitted to?
    – guest271314 Feb 19 '17 at 22:29
  • @guest271314 I don't know what you mean but when you press the submit button it executes the PHP onSubmit function that's how I know that `$_FILES["fileUpload"]["name"];` is undefined. Could that be because it's only executing the function, not actually passing the data into PHP? Yes, that's why I asked this question. @nogad yes, I know, that's why I specified it in the title and tag – Mark Kramer Feb 19 '17 at 22:59
  • @MarkKramer what is the output of var_dump($_FILES) – Charles D Pantoga Feb 20 '17 at 05:08
  • It generates an Alert that contains `
    G:\Wamp\www\October Dev\storage\cms\cache\12\55\test-page-2.htm.php:6:
    array (size=0)
     empty
    
    []`
    – Mark Kramer Feb 20 '17 at 05:55
  • Do you want to use Ajax or that would be find for page to refresh? – B Faley Feb 20 '17 at 12:36
  • Honestly I'm just going to look into using a CMS that has the capability to use php normally. Unfortunately I can't find any that I like anywhere near as much as October. They're all so much less intuitive. (except where it comes to PHP) – Mark Kramer Feb 20 '17 at 19:58

3 Answers3

0

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');
dragontree
  • 1,709
  • 11
  • 14
  • I tried this thoroughly and determined it doesn't work. Similar functions like `Input::get` will work but `Input::file` is always undefined. – Mark Kramer Feb 20 '17 at 06:36
0

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.

LukeTowers
  • 1,262
  • 7
  • 14
0

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

Adam Patterson
  • 958
  • 7
  • 13