1

I have an .xlsx file and a html file with an < input type="file">. I just need upload it and send it to a php file (with js or any other way).

The php file expects an .xlsx file (for this reason I dont parse the .xlsx.) if I load it direct in the php file, works perfectly but I need to upload through an user interface, in this case an html view.

Regards.

Update:

Now the .html looks like this:

<div class="MainContainerPrice">
    <form action="php/excel_to_mysql.php" method="POST">
        <input type="file" name="excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
        <input type="submit">
    </form>
</div>

And the .php looks like this:

<?php 

  include 'simplexlsx.class.php';
  $file = $_FILES['excel'];
  $xlsx = new SimpleXLSX('pricesExcel.xlsx'); //the file directly uploaded that I need to send from html.
  ...

?>

But now I have the next error:

Undefined index: excel in ...\excel_to_mysql.php on line 2.

Why doesn't recognize the name?

2
  • inputs must be in form method="post" container. Check $_FILES on the php side. Commented Jun 15, 2019 at 19:49
  • Thanks for your answer, I updated the code, but looks like doesn't recognize the name, what I'm doing wrong? Regards. Commented Jun 15, 2019 at 20:35

1 Answer 1

1

You need a bit of tweaking in the html and in the PHP part

<div class="MainContainerPrice">
    <form action="php/excel_to_mysql.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
        <input type="submit">
    </form>
</div>

Note the enctype="multipart/form-data". That's needed to actually send the file.

The in the PHP file

<?php 

  include 'simplexlsx.class.php';
  $file = $_FILES['excel']['tmp_name'];
  $xlsx = new SimpleXLSX($file); //the file directly uploaded that I need to send from html.
  ...

?>

$_FILES['excel']['tmp_name'] contains the full path to the uploaded file, note that you can't rely on the name having the '.xlsx' extension, cause the file gets a random name for security purposes.

I strongly suggest you to use the file from within the temporary directory, and to delete it after use.

If the SimpleXLSXclass needs the '.xlsx' extension to work properly, you can try to add it to the temp file

rename($_FILES['excel']['tmp_name'],$_FILES['excel']['tmp_name'].'.xlsx');
Sign up to request clarification or add additional context in comments.

Comments

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.