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
Related
Having spent hours on this, I am out of luck. This script worked perfectly until yesterday. The script generates XML and dumps it as a file (5kb) to a remote FTP server. The script has not changed, nor has our host changed anything. The FTP server company has changed something (they said IP change yesterday) (but claims nothing apart from this). This IP resulted in a different ftp_server which has worked fine.
When I attempt to run the script, I get the following error regardless of whether "ftp_pasv($conn_id, true);" is there or not / disabled:
Warning: ftp_fput() [function.ftp-fput]: Opening ASCII mode data connection in ...
Then it gives the line which contains "FTP_ASCII" below.
When I have the ft_pasv section there only (as per original script), an additional error of the following still appears with the following:
Warning: ftp_fput() [function.ftp-fput]: data_accept: SSL/TLS handshake failed in ...
This is for the same line as the above error.
They were originally on a self-signed SSL. Now, due to my issue, they are now on a 'correct' SSL issued by a well known company. No errors display on Filezilla upon connecting.
Importantly, I can upload via Filezilla with no issues, with or without passive mode
Code above the below code in the script is correct for generating the file as it appears on the script page, once loaded. It just won't dump the file on the server. Here is the connecting to the server bit:
//Connect to the FTP server
$ftp_server = 'import.ftpserverdomain.com';
$ftp_user_name = 'CORRECT-USERNAME';
$ftp_user_pass = 'CORRECT-PASSWORD';
// set up basic ssl connection
$conn_id = ftp_ssl_connect($ftp_server) or die("CONNECTION ERROR");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("LOGIN ERROR");
$directory = ftp_pwd($conn_id); // /
ftp_pasv($conn_id, true);
$fp = fopen($filename, 'w');
fputs($fp,$file_contents);
fclose($fp);
$fp = fopen($filename, 'r');
$path_with_file = $directory.$filename;
if (ftp_fput($conn_id, $path_with_file, $fp, FTP_ASCII)) {
echo "Successfully Uploaded $File\n";
} else {
echo "There was a problem while uploading $File\n";
}
fclose($fp);
unlink($filename);
ftp_close($conn_id);
Any help is much appreciated. Sorry if I lacked any information. I'll be happy to provide any.
It sounds to me like PHP can't find your CA Certs, if you are running this on a linux box, this is something I have encountered before. Some searching should help but basically PHP's OpenSSL integration needs pointing at your cacerts directory so it can use these to validate the SSL connection it is attempting to make.
I fixed this issue. Hopefully this helps someone. The issue turned out to be server (HostGator). They do not allow FTP over TLS on a shared account.
Despite this working for a year, switching hosts resolved the issue.
I am trying to store a file from my local server on an ftp server.
source file is in my current directory (same as php file)
I run the script with http://www.server.co.za/kisv2/xmltest/export.php
file to upload to ftp is http://www.server.co.za/kisv2/xmltest/exportfile.csv
destination ftp path is: ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv
my ftp login defaults to: ftp://ftp.ftpserver.co.za
so I want to copy file from in current directory exportfile.csv to ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv
My Current syntax is:
$source = 'exportfile.csv'; //this is a file in the same directory as my php file. full path is... http://www.server.co.za/kisv2/xmltest/exportfile.csv
$target = '/LocExports/exportfile.csv'; //full path is... ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv
$conn = ftp_connect("ftp.ftpserver.co.za") or die("Could not connect");
ftp_login($conn, "username", "password");
$upload = ftp_put($conn, $target, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }
echo "complete";
This gives me error Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data connection. the file does appear on the FTP server but is blank and 0bytes in size.
Any ideas welcome.
Thanks and regards
UPDATE
$source = 'exportfile.csv';
$target = '/LocExports/exportfile.csv';
$conn = ftp_connect("ftp.server.co.za") or die("Could not connect");
ftp_login($conn, "username", "password");
ftp_pasv($conn, true);
$upload = ftp_put($conn, $target, $source, FTP_BINARY);
if (!$upload) { echo 'FTP upload failed!'; }
echo "complete";
this still fails with:
Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data connection.
file is created on ftp but empty.
thanks again
Add ftp_pasv($conn, true); after your ftp_login(...) .
From http://www.php.net/manual/en/function.ftp-put.php
If when using ftp_put you get the one of the following errors:
Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data
connection
Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data
connection
and it creates the file in the correct location but is a 0kb file and
all FTP commands thereafter fail. It is likely that the client is
behind a firewall. To rectify this use:
ftp_pasv($conn, true);
Before executing any put commands. Took me so long to figure this out
I actually cheered when I did :D
I was having the same problem. The file created but size 0 KB. after setting mode to passive, my file successfully transferred to FTP Server.
InFact there are 3 things we have to take care while uploading file on FTP Server.
set file type to BINARY: objFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
set File Transfer Mode to BINARY: objFtpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
set Mode to Passive (i dont know what it does internally. but it works...!): objFtpClient.enterLocalPassiveMode();
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.
Just now I got an error with my FTP file upload part. I am not able to upload a file via ftp using PHP. the code which I entered is as follows :
<?php
$conn_id = ftp_connect(localhost);
$login_result = ftp_login($conn_id, 'newuser', 'wampp') or die("Could Not Connect To FTP Server");
$image = $_FILES['image']['tmp_name'];
$upload = ftp_put($conn_id, 'sri/image.jpg', $image, FTP_ASCII);
?>
The error that it shows is as follows :
Warning: ftp_put() [function.ftp-put]: Filename invalid in D:\xampp\htdocs\mycloud\edit.php on line 7
Please help me out of this stuff.
I think the process of uploading files via FTP has to be:
Connect to FTP Server
Login to the FTP Server (if applicable)
Change to the right directory - (I believe you need to do this before attempting to upload the file in the sri folder). So you will need to go to the sri folder.
Upload the file (so in your case it should be image.jpg only NOT sri/image.jp )
and then close the connection to the FTP Server.
To change to current directory to the right directory, I think you need to do the following:
if(ftp_chdir($conn_id, "sri"))
{
echo "Current directory is now: " . ftp_pwd($conn_id) ;
}
else
{
echo "Error could not change directory";
}
More info on changing directories
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.