php send file to sftp - php

I am trying to send a file with SFTP I get error while I am trying to upload the file.
Permissions on the remote folder are ok.
$connection = ssh2_connect('10.0.10.245', 22);
ssh2_auth_password($connection, $ftp_user_name, $ftp_user_pass);
$sftp = ssh2_sftp($connection);
echo '<br>';
ssh2_scp_send($connection,$file,"/a.xml", 0644);
print_r(error_get_last());
the error I got is:
Array
(
[type] => 2
[message] => ssh2_scp_send(): Failure creating remote file
[file] => /var/www/FP1/sendFTP.php
[line] => 93
)
Any advise?

You start SFTP session (ssh2_sftp), while you use SCP later for actual transfer (ssh2_scp_send). You definitely do not need the ssh2_sftp line; and it can actually be a cause of your problem.
While technically it is possible to have both SFTP and SCP sessions over one SSH connection, I would not expect PHP to support this. Though I'm not sure.

Are you sure there are no permission problem?
"/a.xml" means you put the file under the root directory /, which won't have write permission for your ftp user usually.

Related

PHP 5.3 FTP JSON file upload using ftp_put Not uploading

I have a json file where I want to upload it to an ftp account on different server.
I have followed all the code of this function but still I get failed Upload!
here is my code:
$con = ftp_connect('ftp.target-server.com');
$login = ftp_login($con, 'usr', 'pa55');
if (!$con || !$login) {
die('Connection attempt failed!');
}
$destination = 'my-target-file.json';
$source = 'my-source-file.json';
ftp_pasv($con, true) or die("Unable switch to passive mode"); //I have tried this option to force passive mode
$upload = ftp_put($con, $destination, $source, FTP_BINARY); //options: FTP_BINARY|FTP_ASCII and I have tried both
if (!$upload){
echo 'Upload failed!<br><br><br>';
}else{
echo "DONE!";
}
ftp_close($con);
I always get failure although everything seems OK.
N.B.
I have tried the access through filezilla ftp client and I managed to
upload the file manually to target destination.
The source server is using older PHP 5.3 but looks fine regarding the ftp_put function.
Also I have tried my-target-file.txt instead of .json and also failed upload.
I have tried couple of different shared hosting serveres as target server but no luck.
I will be grateful if someone can advise!
I have checked the last error using error_get_last(); and got:Array ( [type] => 2 [message] => ftp_put(): Type set to I [file] => /hermes/......../public/ftp.php [line] => 9 ). I can upload files normally through FTP client filezilla to the target server with the same credentials I am usingbut I can not with ftp_put() as it is not working

Cannot connect to external FTPS server with php

I need to extract data from a .gz file which is located on an ftp server. The FTP server works with IP white listing, so I submitted my local IP and my website IP (i used gethostbynameto get the IP) which were both approved.
Locally, I can run this code to reach the file:
$url="ftp://username:password#host/targetfile.gz";
$xml = simplexml_load_file("compress.zlib://$url") or die ("Cannot load file");
This runs perfectly fine and it allows me to extract data from the XML file.
When I run the script on my server however, i'm not getting a connection. I contacted the admin running the ftp server and they told me they only allow FTPS connections.
So, I proceeded with the following code to try and establish a connection:
$conn_id = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
if (!$login_result) {
die("Cannot login.");
}
echo ftp_pwd($conn_id); // /
// close the ssl connection
ftp_close($conn_id);
This also doesn't connect. I'm new to FTP connections with PHP and FTP in general and i've got no clue on how to proceed from this point.
OpenSSL is enabled.
Can anyone point me in the right direction?
Edit: These are the error messages:
When I use the top block of code, this occurs:
Warning: simplexml_load_file(): I/O warning : failed to load external
entity
"compress.zlib://username:password#ftp.thehost.com/targetfile.gz"; in
/path/to/my/website/folder/htdocs/mydomain.com/feeds/script.php on
line 13
When I use the bottom block of code, I get this error:
Warning: ftp_login() expects parameter 1 to be resource, boolean given
in /path/to/my/website/folder/htdocs/mydomain.com/feeds/script.php on
line 16 Cannot login.
I do not have access to the log file afaik
I do not think there's much we can help you here.
You do not have a network connectivity between the web server and FTP server.
Note that it does not have to be because the web server has not been white-listed on the FTP server. It's also possible that the web server does not allow outgoing connections.

ftp_get: Illegal PORT command

On 'localhost' I tried to get a file from FTP server and the local file is successfully created. But when I'm trying in Ubuntu server it's displaying there was a problem and file is not downloading into server. Here is the code. And code file created in this location /var/www/html/
<?php
$local_file = 'whdfcleads.csv';
$server_file = 'hdfc/hdfc_leads.csv';
$ftp_server="*********";
$conn_id = ftp_connect($ftp_server)or die("Couldn't connect to $ftp_server");
$ftp_user_name="*****";
$ftp_user_pass="******";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
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";
}
ftp_close($conn_id);
?>
Please help me to solve this issue, in local host it's working fine but in Ubuntu server local file not creating/downloading.
The error is
Array (
[type] => 2
[message] => ftp_get(): Illegal PORT command
[file] => /var/www/html/wftp.php
[line] => 15
)
The "Illegal PORT command" is a message issued by ProFTPD server, when it receives PORT command with an invalid IP address.
What typically happens, when the client is behind a NAT and reports its internal IP address to the server, not knowing the server is not able to reach back to that IP address.
The easiest (and generally correct) solution is to use an FTP passive mode, instead of an active mode (the default in PHP).
In PHP, you switch to the passive mode by calling the ftp_pasv function after the ftp_login:
...
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
...
See my article on FTP connection modes to understand, what the active and passive mode means, and why everyone uses the passive mode nowadays.

PHP ftp_get failed to open stream when trying to download file

I'm trying to make the browser download a file from an FTP server, but whatever I try, I'm getting this error:
Warning: ftp_get(taak4.docx) [function.ftp-get]: failed to open stream: Permission denied in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231
Warning: ftp_get() [function.ftp-get]: Error opening taak4.docx in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231
I am however 100% sure my FTP server is working fine, as uploading files works correctly. I also set every folder to chmod 777. Does anyone know what the problem might be?
My php code:
$local_file="taak4.dockx";
$server_file="taak4.dockx";
ftp_get($FTPClient->connectionId, $local_file, $server_file, FTP_BINARY);
Thanks in advance !
you must specify the full path to the file. For example:
/var/home/victor/files/taak4.dockx
Use $_SERVER['DOCUMENT_ROOT'] for get document root dir path.
You need to have write permission on the $local_file path. Make it a full path. Example: chmod 777 /test and make $local_file be like /test/taak4.docx.
I was also suffering from this issue even after changing the file permissions on my remote server i was not able to download it on my local server:
Warning: ftp_get(): Can't open Capture.PNG: No such file or directory in C:\MAMP\htdocs\ftp.php on line 25
SOLUTION:
One must include '/' before writing any path in the $server_file variable so the whole example that works just perfect is here:
// This is the path and new file name on my server (name can be different from the remote server file )
// if i want to save it just where my current php file is running ,no need to enter any path just file name
$local_file = 'capture.png';
// This is the path and file name on my Remote server from which i am downloading from
// this should start with '/' and write 'public_html' or 'htdocs' afterwards
$server_file = '/public_html/Capture.PNG';
// ftp details
$ftp_server="example.host.com";
$ftp_user_name="username";
$ftp_user_pass="password";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// This is to so that we do not time out
ftp_pasv($conn_id, true);
// 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";
}
// close the connection
ftp_close($conn_id);

PHP ftp_put warning Warning: ftp_put() [function.ftp-put]: Type set to I. in

When i try to upload files using PHP's ftp_put function, earlier it was erroring:
Warning: ftp_put() [function.ftp-put]: No data connection
Now, i tried to put passive mode on:
ftp_pasv($conn_id, true);
then comes error:
Warning: ftp_put() [function.ftp-put]: Type set to I. in
ftp_login is done properly and it says Successfully.
Now it gives new warning: Warning: ftp_put() [function.ftp-put]: abc.txt: Cannot open or remove a file containing a running program.
Any ideas, why file not tranferring ?
Thanks !
Here is my code snippet:
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("You do not have access to this ftp server!");
if ((!$conn_id) || (!$login_result)) {
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2></span>";
exit;
} else {
//echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}
//turn passive mode on
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $destination_file.$name, $filename, FTP_BINARY);
if (!$upload) {
echo "<span style='color:#FF0000'><h2>FTP upload of $filename has failed!</h2></span> <br />";
} else {
echo 'Uploaded';
}
ftp_close($conn_id);
http://php.net/ftp_pasv
$resource = ftp_connect('ftp.example.com');
ftp_login($resource, 'username', 'password');
# set this to true
ftp_pasv($resource, true);
ftp_get(...);
ftp_put(...);
I was recieving same (not very descriptive) error message E_WARNING ftp_get(): Type set to I..
I found out that it is because server running PHP did not have visible public IP (it is virtual server on my workstation).
Solution was using passive mode. Default setting (active mode) did not have problem on live server, because live server has visible public IP.
The last error you are seeing happens when the FTP daemon is stuck with the uploaded file open and waiting for you to write to it.
Anytime you successfully open a connection over an FTP server, be prepared to close the connection with the following function when the process completes or terminates due to any errors.
ftp_close($conn_id);
It's possible your script is leaving its connections open and the FTP server is getting confused by this. Try adding ftp_close in the appropriate places and see if the script runs more smoothly.
I've tried using the ftp functions in PHP and found it was much easier to use file_put_contents() like the following:
$remote_file = "ftp://username:password#host.com/path/to/file.txt";
file_put_contents($remote_file, $file_contents);
You can still check if it was successful and all that good stuff of course too.
Your ftp setup looks ok, try putting the filename $destination_file.$name in a single variable, dump the variable and make sure this file exists with absolute path if it is not in the same folder as your script. That is the only detail I saw in a quick glance, that could choke your upload.
Make sure your file is not opened in an editor! And if the file is .txt you can use FTP_ASCII although being in binary should not cause a problem.
Good-luck!
I found its solution as below:
I just talked to EUKHOST server support
Main point in this was that the support person now opened a passive port range for FTP on server, and he told us to try the FTP upload now. If you could try it with some testfile and it went through successfully..
Add following lines at the end of
open /etc/vsftpd.conf and add
pasv_promiscuous=YES___ at the end.
In my case, the issue triggering this error was that the file I was trying to upload was too large for the recieving server's configuration.

Categories