简体   繁体   中英

Import excel file through php into mysql database

I facing a problem regarding the import of an excel file into mysql through php. i have used phptriad as a local server to test my web application(site). The application imports the excel file present on the client side computer. the code works fine when accessed through local server.

recently i have registered a subdomain at orgfree.com. i have uploaded all the application files onto the site. but 'importing excel file' function does not work. it shows 'the file D:\\test.xls is not readable' error(test.xls is on my laptop).

what could be the solution to this problem? please help

require_once 'reader.php';
$excelrow=0;$excelcol=0;                    
function parseExcel($excel_file_name_with_path)
{
    global $excelrow,$excelcol;
    $data = new Spreadsheet_Excel_Reader();
    // Set output Encoding.
    $data->setOutputEncoding('CP1251');
    $data->read($excel_file_name_with_path);
    $colname=array('id','name');                    
    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++)
        {
            for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
            {
              $product[$i-1][$j-1]=$data->sheets[0]['cells'][$i][$j];
              $product[$i-1][$colname[$j-1]]=$data->sheets[0]['cells'][$i][$j];
            }
        }
    $excelrow=$i;$excelcol=$j;
    return $product;
}
$data=parseExcel($file);

for($i=0;$i<$excelrow-1;$i++)
{
    for($j=0;$j<$excelcol-1;$j++)
    {
        $fname=ucfirst(strtolower($data[$i][0]));
        $lname=ucfirst(strtolower($data[$i][1]));
        $gender=ucfirst(strtolower($data[$i][2]));
        $phoneno=$data[$i][3];
            $email=strtolower($data[$i][4]);                            
        $occup=ucfirst(strtolower($data[$i][5]));
        $city=ucfirst(strtolower($data[$i][6]));                
    }
        //THEN INSERT INTO DATABASE THROUGH SQL QUERIES                     
}

note: there is only one user to the application ie, Administrator.

Your problem is probably the fact that when running a script on a remote server, you will not have access to your local files any more. This happened to work when the server was running on the local machine, but the remote server has no way of seeing your computer's files.

You would have to add an upload facility. A rudimentary example is outlined in the PHP manual on file uploads.

Alternatively, if you want to use this only for yourself and not as a public service, you could also upload each Excel file to the remote server using FTP, and change the path accordingly to match the new location.

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