PHP Code - import xml file from web server (/public_html/ctrackxml) into mysql database

前端 未结 5 1695
无人共我
无人共我 2021-01-07 02:26

I have xml files been imported onto an FTP server. This is stored in location \'/public_html/ctrackxml/\' with a random file name and in the following format:



        
5条回答
  •  天涯浪人
    2021-01-07 02:58


    "; // Read filenames in current directory looking for XML files $file_arr = array(); if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if (($file != ".") && ($file != "..")) { if(substr($file, -4) == ".xml") { array_push($file_arr, $file); } } } closedir($handle); } // Loop through each XML file in the current directory foreach($file_arr as $filename) { //simplexml load xml file $mess = simplexml_load_file($filename); echo "xml loaded

    "; $messageid = mysql_real_escape_string($mess->messageid); $mobile = mysql_real_escape_string($mess->mobile); $time = mysql_real_escape_string($mess->time); $latitude = mysql_real_escape_string($mess->latitude); $longitude = mysql_real_escape_string($mess->longitude); $status = mysql_real_escape_string($mess->status); $speed = mysql_real_escape_string($mess->speed); $address = mysql_real_escape_string($mess->address); $direction = mysql_real_escape_string($mess->direction); $runningodo = mysql_real_escape_string($mess->runningodo); echo "xml parsed

    "; //insert into databse mysql_query("INSERT INTO xml (messageid, mobile, time, latitude, longitude, status, speed, address, direction, odometer) VALUES ('$messageid', '$mobile', '$time', '$latitude', '$longitude', '$status', '$speed', '$address', '$direction', '$runningodo')") or die(mysql_error()); echo "inserted into mysql

    "; //show updated records printf ("Records inserted: %d\n", mysql_affected_rows()); } //close connection mysql_close($con2); ?>

    The above code should achieve what you are looking for based on the spec. As stated there is one XML record per one XML file.

    This code is designed to run in the same directory as the XML files, you can easily change this by editing opendir command in the code. It will read all the XML files in the current working directory and place the data into the Database.

提交回复
热议问题