How to save xml data to file

I’m using PHP to extract data from a MySQL database. I am able to build an XML file how would I save the XML file to a directory on the server?

below is the code

<?php
include(“config1.php”);

 $trimmed=$_POST['search1'];
 $from_date=$_POST['t1'];
 $to_date=$_POST['t2']; 

header("Content-type:text/xml");
    print("&lt;?xml version=\\"1.0\\"?&gt;");
	//define variables from incoming values



     $posStart = 0;
     $count = 300;
    //create query to products table

$sql=“select vehicle_id,pickup_date,booking_fair,booking_id,customer_name,customer_phno,pickup_point,drop_point,created_on from dispatcher_details
where vehicle_id=‘$trimmed’ AND created_on between '”.$from_date.“’ AND '”.$to_date.“'”;
$sno=1;

    if($posStart==0){
        $sqlCount = "Select count(*) as cnt from ($sql) as tbl";
        $resCount = mysql_query ($sqlCount);
        $rowCount=mysql_fetch_array($resCount);
        $totalCount = $rowCount["cnt"];
    }
     //add limits to query to get only rows necessary for the output
    $sql.= " LIMIT ".$posStart.",".$count;
     //query database to retrieve necessary block of data
    $res = mysql_query ($sql);

     //output data in XML format   
    print("&lt;rows total_count='".$totalCount."' pos='".$posStart."'&gt;");   
	
   while($row = mysql_fetch_array($res)) 
{

	$n3=$row['created_on'];
		$cd=substr($n3,0,10);
		$ct=substr($n3,10,14);


print("&lt;row&gt;");
          
                 print("&lt;cell&gt;");
			print($sno++);
			print("&lt;/cell&gt;");

		print("&lt;cell&gt;");
			print($row['vehicle_id']);
			print("&lt;/cell&gt;");
		  print("&lt;cell&gt;");
			print($row['pickup_date']);
			print("&lt;/cell&gt;");
			print("&lt;cell&gt;");
                print($row['booking_fair']);  //value for product name
            print("&lt;/cell&gt;");

		    print("&lt;cell&gt;");
                print($row['booking_id']);  //value for internal code
            print("&lt;/cell&gt;");
            
	  print("&lt;cell&gt;");
                print($row['customer_name']); //value for internal code
            print("&lt;/cell&gt;");
	
	  print("&lt;cell&gt;");
                print($row['customer_phno']); //value for internal code
            print("&lt;/cell&gt;");

	  print("&lt;cell&gt;");
		print($row['pickup_point']);
	  print("&lt;/cell&gt;");
	print("&lt;cell&gt;");
		print($row['drop_point']);
	  print("&lt;/cell&gt;");
	  print("&lt;cell&gt;");
	  print($cd);
	  print("&lt;/cell&gt;");
	 print("&lt;cell&gt;");
                print($ct); //value for internal code
            print("&lt;/cell&gt;");

	print("&lt;/row&gt;");
        }
	print("&lt;/rows&gt;");

?>

please help me out

thanks

MD. Samiuddin

You should replace print() with a variable (eg $xml) and then use file_put_contents to save it’s content to a file.

should I replace all print with $xml

Yes, you should do something like the following

$xml = "<?xml version=\\"1.0\\"?>";
while($row = mysql_fetch_array($res)) 
{
	// ....

	$xml .= "<row>";

	$xml .= "<cell>";
	$xml .= $sno++;
	$xml .= "</cell>";
	// ...
}
file_put_contents("file.xml", $xml);

I have tried your code but file_put_contents is not writing the data to the file

please help me out
<?php
include(“config1.php”);

 $trimmed=$_POST['search1'];
 $from_date=$_POST['t1'];
 $to_date=$_POST['t2']; 

header("Content-type:text/xml");
    $xml ="&lt;?xml version=\\"1.0\\"?&gt;";
	//define variables from incoming values



     $posStart = 0;
     $count = 300;
    //create query to products table

$sql=“select vehicle_id,pickup_date,booking_fair,booking_id,customer_name,customer_phno,pickup_point,drop_point,created_on from dispatcher_details
where vehicle_id=‘$trimmed’ AND created_on between '”.$from_date.“’ AND '”.$to_date.“'”;
$sno=1;

    if($posStart==0){
        $sqlCount = "Select count(*) as cnt from ($sql) as tbl";
        $resCount = mysql_query ($sqlCount);
        $rowCount=mysql_fetch_array($resCount);
        $totalCount = $rowCount["cnt"];
    }
     //add limits to query to get only rows necessary for the output
    $sql.= " LIMIT ".$posStart.",".$count;
     //query database to retrieve necessary block of data
    $res = mysql_query ($sql);

     //output data in XML format   
    $xml.= "&lt;rows total_count='".$totalCount."' pos='".$posStart."'&gt;";   
	
   while($row = mysql_fetch_array($res)) 
{

	$n3=$row['created_on'];
		$cd=substr($n3,0,10);
		$ct=substr($n3,10,14);


$xml.= "&lt;row&gt;";
          
                 $xml.= "&lt;cell&gt;";
			$xml.= $sno++;
			$xml.= "&lt;/cell&gt;";

		$xml.= "&lt;cell&gt;";
			$xml.=$row['vehicle_id'];
			$xml.= "&lt;/cell&gt;";
		  $xml.= "&lt;cell&gt;";
			$xml.= $row['pickup_date'];
			$xml.= "&lt;/cell&gt;";
			$xml.= "&lt;cell&gt;";
                $xml.= $row['booking_fair'];  //value for product name
            $xml.="&lt;/cell&gt;";

		    $xml.= "&lt;cell&gt;";
                $xml.= $row['booking_id'];  //value for internal code
            $xml.= "&lt;/cell&gt;";
            
	  $xml.= "&lt;cell&gt;";
                $xml.= $row['customer_name']; //value for internal code
            $xml.= "&lt;/cell&gt;";
	
	  $xml.= "&lt;cell&gt;";
                $xml.= $row['customer_phno']; //value for internal code
            $xml.= "&lt;/cell&gt;";

	  $xml.= "&lt;cell&gt;";
		$xml.= $row['pickup_point'];
	  $xml.= "&lt;/cell&gt;";
	$xml.= "&lt;cell&gt;";
		$xml.= $row['drop_point'];
	  $xml.= "&lt;/cell&gt;";
	  $xml.= "&lt;cell&gt;";
	  $xml.= $cd;
	  $xml.= "&lt;/cell&gt;";
	 $xml.= "&lt;cell&gt;";
                $xml.= $ct; //value for internal code
            $xml.= "&lt;/cell&gt;";

	$xml.= "&lt;/row&gt;";
        }
	$xml.= "&lt;/rows&gt;";

file_put_contents(“project/reports/dispatcher_report.xml”, $xml);

?>

please help me out friend

Check that directory “project/reports/” exists and is writeable by the webserver.