I know this question is already asked by many people and I already follow some guide but sorry that till now I can't solve my problem. I have many XML files which are saved on local directory on my FTP server. Now I need to read every XML file line by line and put it on my MySQL database which I have already created. Here I attach my code, which is not run in my server and in the database I didn't see anything.
// Read filenames in current directory looking for XML files
$dir = "/path/to/filename";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (($file !== '.') && ($file !== '..') ) {
$xml = simplexml_load_file($dir.$file);
//$RGSostituzione = $xml->attributes()->Sostituzione;
}
}
closedir($dh);
}
}
// Loop through each XML file in the current directory
$query = array();
foreach($dir as $filename) {
$mess =simplexml_load_file("/path/to/filename".$filename);
$time = mysql_real_escape_string($mess->time);
$Id = mysql_real_escape_string($mess->Id);
$From = mysql_real_escape_string($mess->From);
$Subject = mysql_real_escape_string($mess->Subject);
$address = mysql_real_escape_string($mess->address);
$Body = mysql_real_escape_string($mess->Body);
$query[] = "('$time', '$Id', '$Subject','$address','$Body')";
} if(!empty($query)) {
$query = implode(",",$query);
mysql_query("INSERT INTO xml (time, Id, From, Subject,address,Body)VALUES {$query}") or die("Query 2 non valida: " . mysql_error());
}
//close connection
mysql_close($conn);
?>
Edited _: my last request is from this given code-
// Loop through each XML file in the current directory
$query = array();
foreach($dir as $filename) {
$mess =simplexml_load_file("/path/to/filename".$filename);
after $mess i want to write a if statemnet inside the foreach loop ..if the mess dont read the path print a error? how should i call is statement dont get.if anyone can help me would be greate.