Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

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: est.xls is not readable' error(test.xls is on my laptop).

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 i.e., Administrator.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...