简体   繁体   中英

importing excel file from different location and save it to mysql in php

I am new to php. I tried to importing excel file to mysql, it works well. but i only import excel file from wamp/www/samp.xls. I need to import excel file from different location like D: or E:. How can obtain that, give me a solution. My code for location is wamp/www/samp.xls is:

<html>
  <head>
  <title>Save Excel file details to the database</title>
  </head>
  <body>`enter code here`
    <?php
        include 'db_connection.php';
        include 'reader.php';
        $excel = new Spreadsheet_Excel_Reader();
    ?>
        <table border="1">
        <?php
            $excel->read('samp.xls');
            $x=2;
            while($x<=$excel->sheets[0]['numRows']) {
                $id = isset($excel->sheets[0]['cells'][$x][1]) ? $excel->sheets[0]['cells'][$x][1] : '';
                $name = isset($excel->sheets[0]['cells'][$x][2]) ? $excel->sheets[0]['cells'][$x][2] : '';
               $age = isset($excel->sheets[0]['cells'][$x][1]) ? $excel->sheets[0]['cells'][$x][3] : '';
                $email = isset($excel->sheets[0]['cells'][$x][2]) ? $excel->sheets[0]['cells'][$x][4] : '';
                // Save details
                $sql_insert="INSERT INTO students (sid,name,age,email) VALUES ('$id','$name','$age','$email')";
                $result_insert = mysql_query($sql_insert) or die(mysql_error());
              $x++;
            }
        ?>
    </table>
  </body>
</html>

use a basic form

<form enctype='multipart/form-data'><input type="file" name="excel" /> <input type="submit" value="upload" /></form>

and on the php side handle the form as follows

$excel->read($_FILES['excel']['tmp_name']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM