Taking xlsx file with HTML and PHP

Asked

Viewed 3,538 times

1

I need to import data xlsx to a system I created.

HTML

<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="arquivo">
    <input type="submit" name="pegar value="pegar">
</form>

PHP

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

if (isset($_POST['pega'])) {

   $arquivo = $_FILES['arquivo'];
   $file = fopen($arquivo,"r");

   while(! feof($file)){
       echo fgets($file). "<br />";
   }

   fclose($file);
}

And these are the error messages that appear:

Warning: fopen() expects Parameter 1 to be a Valid path, array Given in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importaCSV.php on line 20

Warning: feof() expects Parameter 1 to be Resource, Boolean Given in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importaCSV.php on line 22

Warning: fgets() expects Parameter 1 to be Resource, Boolean Given in /Applications/MAMP/htdocs/sistemas/scripts_da_web/php/importaCSV.php on line 24

  • Upload the file before opening, then you delete it with unlink()

  • Sorry @rray, I don’t understand. Upload before?

  • Yes, use move_uploaded_file, remember that $_FILES is an array, of a print_r() to see the information contained in it.

  • With print_r() appeared this: Array ( [name] => VP 1015.xlsx [type] => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet [tmp_name] => /Applications/MAMP/tmp/php/phpCIgXr7 [error] => 0 [size] => 76347 )

  • Can someone please help me?

1 answer

2


It is easier for you to do this using the standard CSV files (comma separated). Use the Phpexcel library to convert your xlsx file to csv, and then use the fgetcsv function to transform the csv data into an array in php. Install lib and complete your code with something like this:

$file = $_FILES['arquivo']['tmp_name'];
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$csvFileName = str_replace('.xlsx', '.csv', $file);
$objWriter->save($csvFileName);
if (($handle = fopen($csvFileName, "r")) !== false) {
    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
        $num = count($data);
        echo "<p> $num campos na linha $row: <br /></p>\n";
        $row++;
        for ($c = 0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

Useful links:

https://github.com/PHPOffice/PHPExcel

http://php.net/manual/en/function.fgetcsv.php

  • Whence $file?

  • $file would be the full path of the xlsx file that you want to treat

  • In his case the file comes via $_FILES.

  • I’ll try that later @Lfziron. Thanks. But then I’ll have to download the Phpexcel class, right?

  • Just give a include of the Phpexcel.php class and put the code above @Lfziron?

  • @Lfziron, I called the class, and I put your code, but the following warning appears on the screen: "Warning: file_exists() expects Parameter 1 to be a Valid path, array Given in /Applications/MAMP/htdocs/sistemas/scripts_da_web/Phpexcel/Classes/Phpexcel/Reader/Excel2007.php on line 342 Notice: Array to string Conversion in /Applications/MAMP/htdocs/sistemas/scripts_da_web/Phpexcel/Classes/Phpexcel/Reader/Excel2007.php on line 343 "

  • Can someone please help me?

  • Put before the code I gave you: $file = $_FILES['file']['tmp_name'];

  • I didn’t see that you had answered. I had already done that. Now it’s working, the problem is that the data of each cell is displayed, one below the other on the screen and should be appearing 3 data, one next to the other and so on, understand?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.