Pravin Pravin - 16 days ago 6
PHP Question

Send a Xml file created using DOMDocumet through ftp in php

XML file creation:

$doc = new DOMDocument();
$doc->formatOutput = true;

$root = $doc->createElement('StudentDetails');
$root = $doc->appendChild($root);

$ele1 = $doc->createElement('StudentName');
$ele1->nodeValue=$sname;
$root->appendChild($ele1);

$ele2 = $doc->createElement('DateOfBirth');
$ele2->nodeValue=$dob;
$root->appendChild($ele2);

$ele3 = $doc->createElement('ResidentialAddress');
$ele3->nodeValue=$address;
$root->appendChild($ele3);
$abc =date('d-m-Y').'_'.$create.'.xml';

$xyz=$doc->save($abc);


Send the saved XML file through FTP:

$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";


$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
//die();

if(!$filename)
{
echo"Please select a file";
}

else
{
ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}


I want to send the XML file created using
DOMDocument
to a FTP server but I am not able to fetch the file saved after
DOMDocument
and upload each file on the go to the FTP server.
***EDITS

Answer

Most typical cause of problems with ftp_put is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the transfer working. Use the ftp_pasv function.

$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization Failed");
// turn passive mode on
ftp_pasv($connect, true);

See also: