How to use FTP to connect to Azure scale set Instance - php

So i have created a scale set in Azure (2 windows server 2016 VMs). I want to have a PHP application running on them. I want to know if it is possible to use an FTP connection to remotely upload/edit my php files which are going to be on the VMs. If not, what are the others ways i can use to remotely edit/upload my php files?. This is my first time working with a scaleset. thanks

Yes it's possible to use FTP in php:
//open a connection
$conn = ftp_connect($host);
//login to server
ftp_login($conn, $user, $pass);
//upload file
if (ftp_put($conn, $remoteFile, $localFile, FTP_ASCII)) {
return "<p>successfully uploaded $localFile to $remoteFile</p>";
} else {
return "<p>There was a problem while uploading $remoteFile</p>";
}
See the manual for more FTP functions http://php.net/manual/en/book.ftp.php

Related

Copy files from ftp server using php

I am trying to come up with a scehduled copy code with php.
I dont have problem copying from shared folder via eg //xxx.xx.xxx/folder/filename.csv ( remote computer ). I have some files located at a ftp server. How can I do the same?
Appreciate any help
$file = 'ftp://xx.xx.xxx.xx/Log/123.csv';
$newfile = 'spc/ems_files/123.csv';
if ( copy($file, $newfile) ) {
echo "Copy success!";
} else {
echo "Copy failed.";
}
Additional info
Bask on the feed I used php ftp function to connect to the ftp using below code
based it said failed to connect. I can connect it with a FTP software like filezilla without much issue. What is wrong ?
$ftp_server = "10.76.170.123";
$ftp = ftp_connect("10.76.170.123");
if (!$ftp) die('could not connect.');
// login
$r = ftp_login($ftp, "anonymous", "");
if (!$r) die('could not login.');
// enter passive mode
$r = ftp_pasv($ftp, true);
if (!$r) die('could not enable passive mode.');
// get listing
$r = ftp_rawlist($ftp, "Log");
var_dump($r);
It looks like what you're doing should be possible, as long as the FTP server supports passive mode and you are using the correct credentials. I would double-check the path to the file, on the FTP server, is correct too.
http://php.net/manual/en/wrappers.php
http://php.net/manual/en/wrappers.ftp.php
Here are some other solutions you could try ...
SFTP using phpseclib - http://phpseclib.sourceforge.net/sftp/examples.html
FTP Functions in PHP - http://php.net/manual/en/book.ftp.php
I solved the problem.default ftp connection used email address as password
I used this to connect to the ftp successfully
$r = ftp_login($ftp, "anonymous", "anonymous#domain.com");
So I chose to use php FTP function to do the downloading task

php big upload via ftp

i have to trasfer file via ftp with php. The files are big (also over 500MB).
So i think use php with ftp.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
I have to know if the timeout is considered also in ftp trasfer.
If i trasfer the file via php page like upload.php with the code write, i have the execution time limit setting on the web server ?
You must take too many considerations before doing this operation, as mentioned below the first thing you need to check if your FTP server allows 500MB for uploads, the second thing you must set some PHP directives like post_max_size and max_upload_size, I suggest to use an FTP library to make it easier to do that, this a great one php-ftp-client that I've build.
Enable the feature on server side, which allows resuming file uploads.
From php you might be able to call the ftp with command_line options using functions like system(), or exec().
And, yes, in php there are limits: max_upload_size, max_execution_time, max_post_size and etc.
You will have to split the whole process into steps, with reloading of the page - if needed.

PHP ftp_nlist() - timeout

I have strange problem: I can connect to ftp server, create directory but I can not get a list of files in directory.
What could be wrong?
Facts:
Server is behind firewall
Yes, I use passive mode, as you can see from code
PHP script is on the same server where FTP server is located
I can access FTP from my desktop using FTP client, and everything works normal, I can create/upload and read contents of directories.
ftp_pwd() and ftp_mkdir() work fast, ftp_nlist() and ftp_rawlist() wait for time-out specified in ftp_connect() (10 seconds in example case)
In active mode - almost the same results (red-dir-functions fail immediately)
I think that problem not in the script but in ftp/server/firewall/access rights setup.
There is my code example:
$conn = ftp_connect('my.host.here', 21, 10);
if (!$conn) {
throw new Exception('Unable to connect to FTP');
}
$login_result = ftp_login($conn, 'login', 'password');
if (!$login_result) {
throw new Exception('Unable to login to FTP');
}
// This is important part, because my server is behind frirewall/nat.
$paswRes = ftp_pasv($conn, true);
if (!$paswRes) {
throw new Exception('Failed to enable passive FTP mode');
}
$resPwd = ftp_pwd($conn); //Returns: "/"
$resMkdir = ftp_mkdir($conn , 'testDir'); //Returns: "/testDir", creates new dir
$resNlist = ftp_nlist($conn, '.'); //Returns: bool(false)
$resRawlist = ftp_rawlist($conn, '.'); //Returns: bool(false)
Upd:
It works if host is localhost. So looks like problem is in firewall/ftp server setup.
I use ProFtpd on Amazon EC2
Passive ports specified as PassivePorts 49152 65534
This range has been added as inbound port range in Amazon EC2 Security Groups 49152 - 65534 0.0.0.0/0
Ok, I have solved the problem.
It was FTP server misconfiguration (in my case - ProFTPD server):
Besides PassivePorts I had to specify MasqueradeAddress
MasqueradeAddress <your-Ftp-Server-Domain-Name-Or-Ip-Address>
Upd: Also, check this article: http://www.elitehosts.com/blog/php-ftp-passive-ftp-server-behind-nat-nightmare/

Cannot connect using ftp_connect()

I'm developing a web page using Microsoft WebMatrix and everything was going smoothly and working.
The problem I have is related to ftp_connect() in php.
I made several tests using this same program in my computer, but when I tried through the web page to make a file transfer, the ftp_connet() function did not work.
I've all ready searched and can't find a solution to this error, because I can connect with a ftp client, and have access through a webpage and can connect directly from my computer, but the not from the web.
Here is the relevant code:
$conn_id = ftp_connect(FTPSERVER);
$login_result = ftp_login($conn_id, FTPUSER, FTPPASS);
if(ftp_put($conn_id,$path_file_ftp_gr,$filegr_path,FTP_BINARY)) {
if (ftp_put($conn_id,$path_file_ftp_pq,$filepq_path,FTP_BINARY)){
$query_result = mysql_query($query_add);
ftp_close($conn_id);
echo 'Sucesso<br>';
ftp_close($conn_id);
}
}
else {
echo 'falhou<br>';
}
You're not by chance passing an integer to the FTP port parameter are you? For me this doesn't work:
$ftp = ftp_connect('myserver.com',51);
But this does:
$ftp = ftp_connect('myserver.com','51');

php script on Windows hosting does not list ftp files (ftp_rawlist issues)

I'm trying to list some files from an external FTP server using php ftp functions on a Windows shared hosting, but I'm having several problems.
I firstly tried with a couple of web applications like ajaxplorer and net2ftp, but I got frustrated and I decided to make a very basic script for testing..
<?php
$ftp_server = "alinuxftpserver";
$ftp_user = "user";
$ftp_pass = "pass";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// change temp folder (windows)
putenv("TMP=D://inetpub//webs//domain//net2ftp//tmp");
echo getenv('TMP');
// try to login
if (#ftp_login($conn_id, $ftp_user, $ftp_pass))
{
echo "Connected as $ftp_user#$ftp_server\n";
}
else
{
echo "Couldn't connect as $ftp_user#$ftp_server\n";
}
if(ftp_pasv( $conn_id, true ))
echo "Passive mode, it worked<br/>";
else
echo "Passive mode, it didn't work<br/>";
$contents = ftp_rawlist($conn_id, ".");
var_dump($contents);
ftp_close($conn_id);
die;
?>
On my localhost (linux) it returns an array, while on the windows hosting it returns:
Warning: ftp_rawlist() [function.ftp-rawlist]: php_connect_nonb() failed: No such file or directory (2) in D:\inetpub\webs\domain\ftp.php on line 26
bool(false)
Can't understand.. the directory should be "/" on the external ftp server and of course there are some files & folders (2 folders and 1 file).. In fact on my MAMP installation it works well.
Hosting guys told me that the server configuration is ok.
use ftp_pasv($conn_id, true); some ftp connections will work in passive mode only
I'm not 100% sure, but I guess, you should use $contents = ftp_rawlist($conn_id, "/");
instead of $contents = ftp_rawlist($conn_id, ".");
Check your FTP server logs. In my case, the pasv_address was set to a wrong IP address.
Better late than never... I had the same problem. With a Linux server everything worked great, but with Windows Server (many versions) we had many problems, including with ftp_nlist() returning an empty array. This worked for us, but I don't know why!
ftp_nlist($handler, '*');

Categories