-1

I'm using this code to input a file:

<label class="control-label">Select File</label>
<input id="input-1" type="file" class="file">

But how can i save the content of the input file into a php array and output the content?

The file is something like this:

name1
name2
name3
1pa
  • 715
  • 2
  • 9
  • 23
  • You need to make two steps from this. Do you want to immediately parse the file/asynchronus (in this case, ajax?). If not. Just upload the file. The file will be placed in the PHP's `tmp` folder and you can parse it. Like with `file_get_contents()` – Matt Backslash May 29 '17 at 15:25
  • i don't need ajax. When i'm doing this `$file = file_get_contents('./test.txt', FILE_USE_INCLUDE_PATH); ` and `var_dump($file);` i'm getting `bool(false)` – 1pa May 29 '17 at 15:39
  • you should have posted the full html for this, including the php, since you tagged as such. There's most likely a duplicate for it already, given you didn't name your input and would have clearly thrown an undefined index notice. – Funk Forty Niner May 29 '17 at 15:50

1 Answers1

1

First, give a name to file field, like:

<input id="input-1" name="uploaded-file" type="file" class="file">

Then, use $_FILES superglobal in your php file to access it:

$content = file_get_contents($_FILES['uploaded-file']['tmp_name']);

Read PHP: Handling file uploads for more info.

kopaty4
  • 2,236
  • 4
  • 26
  • 39
  • when i ´var_dump($content);´ it's return ´bool(false) ´ – 1pa May 29 '17 at 15:34
  • It is necessary to see the complete code to understand what is wrong in your case. I can try to assume that when the form is declared, you are not specifying enctype property: for file uploading it should be set like this: `
    `
    – kopaty4 May 29 '17 at 15:38
  • Ǹice! It was that! Thank you very much! – 1pa May 29 '17 at 15:41