I need to write a script that will download the XML from FTP copied files to the server that will be added to the database by the script. The problem is that I do not know if it is possible to connect to the server where the database is placed and copy files to a folder from which they can be downloaded?
The database is installed on synology MariaDB 10 (localhost via UNIX socket).
<?php
$ftp_serwer = "xxx";
$ftp_nazwa_uzytkownika = "xxx";
$ftp_haslo = "xxx";
// nawiązanie połączenia lub zakończenie działania skryptu
$conn_id = ftp_connect($ftp_serwer) or die("Nie można połączyć się z $ftp_serwer");
// próba logowania
if (#ftp_login($conn_id, $ftp_nazwa_uzytkownika, $ftp_haslo)) {
echo "Połączony jako $ftp_nazwa_uzytkownika#$ftp_serwer\n";
} else {
echo "Nie można zalogować się jako $ftp_nazwa_uzytkownika\n";
}
// nawiązanie połączenia z baza danych msql
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "zlecenia";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, 3307);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// define some variables
$local_file = 'Z:\htdocs\phpmysql\TEST\CRM\localfile\yahootable.xml';
$server_file = '/TU WGRYWAC DANE/yahootable.xml';
// set up basic connection
$conn_id = ftp_connect($ftp_serwer);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_nazwa_uzytkownika, $ftp_haslo);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
$sql = "LOAD DATA LOCAL INFILE
'Z:/htdocs/phpmysql/TEST/CRM/localfile/yahootable.xml'
INTO TABLE
yahootable
CHARACTER SET 'utf8'
LINES STARTING BY '<row>' TERMINATED BY '</row>'
(#tmp)
SET
id = ExtractValue(#tmp, '//id'),
various = ExtractValue(#tmp, '//various'),
message = ExtractValue(#tmp, '//message')
";
// zamknięcie połączenia
ftp_close($conn_id);
mysqli_close($conn);
?>
So my questions are:
Where do I have to upload a file so that the server can download it using
LOAD DATA INFILE?
How to copy files to this folder using a script?
When you use the LOCAL keyword in your LOAD DATA LOCAL INFILE command it will load the file from the host where the MySQL client is running, as described on https://dev.mysql.com/doc/refman/8.0/en/load-data.html:
If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full path name to specify its exact location. If given as a relative path name, the name is interpreted relative to the directory in which the client program was started.
The MySQL client in your case will be the host which runs the php script. The path has to be accessible by that host and will automatically be transferred to the MySQL server.
Related
I upload 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/>";
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.
The ftp_put returns false.
Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) 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) or die("Unable switch to passive mode");
The ftp_pasv must be called after ftp_login. Calling it before has no effect.
See also:
PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command (what is quite common, if the server is behind firewall/NAT), you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
This worked:
// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";
// upload file
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
echo "Successfully uploaded $file.";
} else {
echo "Error uploading $file";
}
// close connection
ftp_close($ftp_conn);
I upload 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/>";
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.
The ftp_put returns false.
Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) 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) or die("Unable switch to passive mode");
The ftp_pasv must be called after ftp_login. Calling it before has no effect.
See also:
PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
my article on the active and passive FTP connection modes.
Further, if your FTP server is reporting an incorrect IP address in the response to the PASV command (what is quite common, if the server is behind firewall/NAT), you might need to workaround it by using:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
See PHP FTP + Passive FTP Server Behind NAT.
Though the right solution in this case, is to get the server fixed.
This worked:
// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";
// upload file
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
echo "Successfully uploaded $file.";
} else {
echo "Error uploading $file";
}
// close connection
ftp_close($ftp_conn);
I have a web form that I will be hosting on a centOS web server. The code that I have written, will take the data that has been entered, and save it to an excel spreadsheet to a specific location. i also have a file server, this is where I would like to have the file saved to. this is the first time that i have done a server to server file save/copy/transfer. on the web server, would i have to install an ftp client to accomplish this? When testing the program locally on my computer, this is how i currently have the destination path set up.
file_put_contents('C:\Users\username\Box Sync\Driver Check In\Shortage'.
$month. '\Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
with the code being hosted on the web server, how can i change the destination path to point to my file server?
Here is a sample script of how to upload file from your localhost to any FTP server.
Source: PHP Manual
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($resource, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>
I set up my IIS Server in Windows and set a list of folders that should only be downloaded. Now i connected to the server using php too.
$username=$_POST["username"];
$password=$_POST["paswd"];
$host="localhost";
$ftpcon= ftp_connect($host) or die("could not connect");
$login=ftp_login($ftpcon,$username,$password);
Now what i want to list is the windows directory. That will help me navigate like the file explorer in windows. Would you mind giving me some help here
Try the below code to list all your files & directories:
<?php
$ftp_server = 'xxx.xx.xx.xx';//Replace with your IP
$conn_id = ftp_connect($ftp_server);
# login with username and password
$user = 'ftp_user_name'; //Replace with your FTP User name
$passwd = 'ftp_password'; //Replace with your FTP Password
$login_result = ftp_login($conn_id, $user, $passwd);
# check connection
if (!$conn_id)
{
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $user";
exit();
}
else
{
$cwd = ftp_pwd($conn_id);
$contentList = ftp_nlist($conn_id, "."); // This denotes the current path, you can replace this with your actual path
foreach($contentList as $contentListItem)
{
if (ftp_size($conn_id, $contentListItem) == -1)
{
#Its a direcotry
echo "Directory : ".$contentListItem."<br>";
}
else
{
#Its a file
echo "File : ".$contentListItem."<br>";
}
}
}
?>
Can someone figure out what this is not working? I kept trying different things, double checking syntax etc but I get the error message I have setup for when a file does not transfer. This is my first PHP script.
I appreciate any help I will do research in the meantime.
Using NetBeans
<?php
$user = "username";
$password = "password";
$server_file = 'mag_hounddog.mp3';
$local_file = 'C:\users\kazuiman\desktop\mag_hounddog.mp3';
$ftp_server = "ftp.mysite.com";
// setup a basic connection to the ftp server
$connectionID = ftp_connect($ftp_server) or die("Connection was Not Successful");
// login with username and password for your account
$loginResult = ftp_login($connectionID, $user, $password);
echo ftp_pwd($connectionID);
// changes the directory to the tvstation you can hard code one in here.
if (ftp_chdir($connectionID, "test1nbiiwcdp_apps/tv2beta/streams/_definst_/")) {
echo "Current Directory is Now..." . ftp_pwd($connectionID) . "\n";
} else {
echo "Error Changing Directory .. perhaps it doesn't exist?";
}
// Now to download the files and put them onto your local machine
if (ftp_get($connectionID, $local_file, $remote_file, FTP_BINARY)) {
echo "Succesfully written $server_file to $local_file\n";
} else {
echo "There was a problem with the FTP transfer please check script\n";
}
// close the connection
ftp_close($connectionID)
?>
You have never defined the variable $remote_file.
In your preamble it is called $server_file.
Is this a copy-paste-error?