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
Related
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
I am trying to download from FTP server (using FileZilla server) to my local computer, but I keep getting error
This is my code
<?php
// connect and login to FTP server
$ftp_server = "127.0.0.1";
$ftp_username = "myusername";
$ftp_userpass = "mypassword";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "success connect to FTP";
$local_file = "C:\Users\PROSIA\Desktop\test.docx";
$server_file = "C:\Users\PROSIA\Documents\dummyfile.docx";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
ftp_close($ftp_conn);
This is the error I get:
Warning: ftp_get(C:\Users\PROSIA\Desktop est.docx): failed to open stream:
Error downloading C:\Users\PROSIA\Documents\dummyfile.docx.
My logic for the code is $local_file is the path to save the downloaded file to my local computer and $server_file is the path from FTP server to download the file
So I am a bit confused with the first warning, "failed to open stream" while the file is not exist yet and its seem got blank space (it should be \Desktop\test.docx rather than Desktop est.docx)
And additional question, can I just read, without download?
You cannot use absolute paths to remote files in FTP protocol.
The FileZilla FTP servers has mapping in its configuration that projects the remote file system into virtual FTP file tree. You have to use paths to the virtual file tree.
E.g. The C:\Users\PROSIA can be mapped to something like /users/PROSIA. In that case you use:
$server_file = "/users/PROSIA/dummyfile.docx";
Chances are that you have actually not configured the mapping at all. So you cannot really access the file, until you do the mapping.
Start by connecting to the FTP server with some GUI FTP client, and try to locate the file. The client will show you the correct path to use in your code.
The next problem you have, is that you need to enable the FTP passive mode. The default active mode will hardly work, if you are behind a firewall on NAT. See my article on network configuration needed for active and passive FTP modes.
To switch to the passive mode, use the ftp_pasv function.
ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn, true);
And yes, you can read without downloading.
See
PHP: How do I read a .txt file from FTP server into a variable? or
Stream FTP download to output
I'm trying to set up a ssh connection, and later a sftp connection to transfer file via FTP to my server. I have used FileZilla to upload files successfully before.
These are my login terminal command and FileZilla login information:
ssh b0xxxxx#linux7.aa.bb.cc.dd
Host: sftp://linux7.aa.bb.cc.dd
Username: b0xx
Port: 22
Here is my php code:
<?php
$ftp_server = "b0xx#linux7.aa.bb.cc.dd";
$ftp_port = 22;
$ftp_user = "b0xx";
$ftp_pass = "##";
$connection = ssh2_connect($ftp_server, $ftp_port);
echo "Successful!\n";
if (ssh2_auth_password($connection, $ftp_user, $ftp_pass)) {
echo "Successful\n";
} else {
die ('Failed...');
}
$sftp = ssh2_sftp($connection);
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>
Question 1: Can I just use ftp functions to upload/download file from my server?
Or do I must set up ssh?
Question 2: When I execute the code, the website only displays
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
which means that the code does not execute connection successfully. Does anyone know the cause of the bug?
Very much appreciate the help.
Seems SSH2 PECL extension is not installed. Install the extension and then try your code.
$ftp_server = "b0xx#linux7.aa.bb.cc.dd";
"b0xx#linux7.aa.bb.cc.dd" isn't a hostname. Hostnames can't have "#" in them. This is probably intended to be a "username#hostname" string, but ssh_connect() doesn't accept a string in this form.
Remove the username portion:
$ftp_server = "linux7.aa.bb.cc.dd";
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, '*');
how can i do extract zip file for script setup in safe_mode on
i'm trying this
require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if (($v_result_list = $archive->extract()) == 0) {
die("Error : ".$archive->errorInfo(true));
}
echo "<pre>";
var_dump($v_result_list);
echo "</pre>";
but i get OWNER error on the extracted dir
How do i fix this problem, or i think re connect to this ftp again and upload and extract file to this ftp
$local_file = './arcive.zip';
$ftp_path = '/extract';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
How do you think i can do?
when a file is unzipped it preserves the owner and permissions information. In your case it's likely that the permissions do not allow world access. if the owner of the file is different from the account that PHP runs under, you cannot chown the file or change permissions. Unless PHP is run as root (and who does that?). in safe mode you can't even do that as root.
Ask the person who archives the file to make it "read & write" for everybody.
As for the second part of the question, you cannot extract something on another server over ftp protocol. you'd have to ssh into the server.
http://phpseclib.sourceforge.net might be of help in that