简体   繁体   English

通过PHP将Excel文件导入MySQL数据库

[英]Import excel file through php into mysql database

I facing a problem regarding the import of an excel file into mysql through php. 我面临有关通过php将excel文件导入mysql的问题。 i have used phptriad as a local server to test my web application(site). 我已经使用phptriad作为本地服务器来测试我的Web应用程序(站点)。 The application imports the excel file present on the client side computer. 该应用程序导入客户端计算机上存在的excel文件。 the code works fine when accessed through local server. 通过本地服务器访问时,代码工作正常。

recently i have registered a subdomain at orgfree.com. 最近,我在orgfree.com上注册了一个子域。 i have uploaded all the application files onto the site. 我已将所有应用程序文件上传到该站点。 but 'importing excel file' function does not work. 但是“导入excel文件”功能不起作用。 it shows 'the file D:\\test.xls is not readable' error(test.xls is on my laptop). 它显示“文件D:\\ test.xls无法读取”错误(test.xls在我的笔记本电脑上)。

what could be the solution to this problem? 该问题的解决方案是什么? please help 请帮忙

Here is the code 这是代码

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. PHP手册中概述了有关文件上传的基本示例

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. 另外,如果您只想自己使用而不是作为公共服务使用,则还可以使用FTP将每个Excel文件上传到远程服务器,并相应地更改路径以匹配新位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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