PHP FTP Not Working Using Passive Mode - php

I have a script that was working before, but we recently changed the FTP location and is no longer working correctly. I have tried setting to passive mode and switching between ascii / binary with no luck.
Below is my script, is there something obvious I'm missing? Is there a way to easily identify the error? The specified directory has all permissions. It connects just fine and I can list the directory contents and see all the folders correctly...just doesn't upload.
$file = $createdAt.".txt";
$fp = fopen('exportorder/'.$file, 'r');
$conn_id = ftp_connect('ftp.location.com') or die("Couldn't connect to ftp.location.com");;
$login_result = ftp_login($conn_id, 'username', 'pass');
ftp_pasv($conn_id,TRUE);
ftp_chdir($conn_id, '/OTD-0000117');
if (ftp_fput($conn_id, 'exportorder/'.$file, $fp, FTP_BINARY)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
Thanks in advance!

Use ftp_put that will dump the file from source to target if there is no change between source and target file.

Related

ftp_put(): Can't open that file: No such file or directory

I search a lot on internet but i don't find a solution.
I need upload a file to an FTP server through PHP.
I tested various script PHP like this below but I always receive the same problem (I tries with absolute, normal and other path):
connected
Warning: ftp_put(): Can't open that file: No such file or directory in /web/htdocs/www.stanem.it/home/csv/importinnovacsv.php on line 20
There was a problem while uploading /web/htdocs/www.stanem.it/home/csv/test.csv
What I have to do?
<?php
$ftp_server="ftp.xxxx.it";
$ftp_user_name="user";
$ftp_user_pass="psw";
// connect and login to FTP server
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
if($login) {
echo 'connected<br>';
// Where I need to put file
$local_file = '/web/htdocs/www.stanem.it/home/csv/test.csv';
// Where I copy the file
$server_dir = 'ftp://15886:XDBbcFYQUs#ftp.innovaspa.it';
// upload a file
if (ftp_put($ftp_conn, $server_dir, $local_file, FTP_ASCII)) {
echo "successfully uploaded $local_file\n";
exit;
} else {
echo "There was a problem while uploading $local_file\n";
exit;
}
}
The $remote_file argument of ftp_put is a path to the file on the FTP server.
You are passing a URL (and it even misses any path or file name).
It should be like:
$remote_file = "/remote/path/test.csv";
Once you resolve the path problem, you will also likely face data connection problems, as your are using the default active mode. See PHP ftp_put fails.

php downloading a file from ftp doesn't work locally using XAMPP works only with older files on ftp

When I run the code below the local file gets deleted, I cannot work out why:
$local_file = 'test.csv';
$server_file = 'test_directory/test2.csv';
$ftp_server="servername";
$ftp_user="test";
$ftp_pass="pass";
$conn_id = ftp_connect($ftp_server);
// login with username and password`
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
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);
So I get an error on the console showing cannot open no file or directory.
Edit:
Renaming the file to something else other than I am getting from ftp does not delete the file but still doesn't work.
I am running this in XAMMP htdocs location.
Edit:
Problem is if I add a new file to the ftp server, it works with existing ones even though the permission is the same.
Problem is a corrupt file on the ftp server. I tried with a different file and it works.

Uploading file with ftp_nb_put to FTP server in PHP results in an empty destination file

Hello,
I'm doing a script to send files directly to a server by ftp protocol.
To do that, I use php ftp functions.
$conn_id = ftp_connect('XXXX.com', 21);
$login_result = ftp_login($conn_id, 'USER_XXXX', 'PWD_XXXX');
ftp_pasv($conn_id, true) or die("Cannot switch to passive mode");
if ((!$conn_id) || (!$login_result)) {
die("Problem FTP connection !");
}
if (ftp_chdir($conn_id, 'SERVER_PATH')) {
echo ftp_pwd($conn_id) . "\n";
} else {
echo "Problem to change path\n";
}
ftp_nb_put($conn_id, $File, $localPath. $File, FTP_ASCII);
ftp_close($conn_id);
No error is announced, but the file that arrives on my server is empty.
I forgot to precise, my file to upload is a big xlsx file.
Your code is just wrong. You use non-blocking ftp_nb_put, as if it were blocking.
If you want a simple code, you have to use ftp_put.
If you really need a non-blocking code, you have to close the connection only after ftp_nb_continue reports FTP_FINISHED. See File uploaded with ftp_nb_put to FileZilla FTP server in PHP is corrupted.
Otherwise your current code will just close the connection at a moment the upload hardly even started.
The "sleep", suggested in comments, is (as mentioned there) just for a test, you cannot reliably use it in a real code.
Your second problem is that you are uploading binary .xlsx files in a text/ascii mode (FTP_ASCII). You have to use a binary mode (FTP_BINARY). The text/ascii mode will corrupt binary files. So even, if the upload finishes, the file will be corrupted.
This will work:
ftp_put($conn_id, $File, $localPath.$File, FTP_BINARY);

File won't upload to ftp server

I'm trying to upload a file from my webserver to my game server through a script. The problem is that it can't find the directory.
The full directory is /174.34.132.106 port 27015/tf/addons/sourcemod/configs/tf2items.weapons.txt
This path didn't work so I asked the hosting about it and they insisted that /tf/addons/sourcemod/configs/tf2items.weapons.txt is the correct path but this doesn't work either. The game server is running on a windows server and i'm pretty sure the web server is running on linux. Is my code wrong, do I have to replace the spaces in the directory with %20. Thanks in advance!
$ftp_server="174.34.132.106";
$ftp_user_name="Username";
$ftp_user_pass="Password";
$remote_file = "tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
if(!file_exists($file)) echo "The local file does not exist";
$conn_id = ftp_connect($ftp_server) or die('Unable to create the connection');
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_chdir($conn_id, "174.34.132.106 port 27015/tf/addons/sourcemod/configs/");
echo ftp_pwd($conn_id);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
I noticed that the FTP server you're connecting to is using a non-standard port, so it's probably not making the connection. You need to specify the port in the ftp_connect, like so:
$ftp_server="174.34.132.106";
$ftp_port = "27015";
$ftp_user_name="username";
$ftp_user_pass="password";
$remote_file = "tf/addons/sourcemod/configs/tf2items.weapons.txt";
$file = "weapons/tf2items.weapons.txt";//tobe uploaded
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die('Unable to create the connection');
The die() will stop the script if it's unable to make the connection. You can add the same after your ftp_login line to make sure that it's actually logging in.
Edit To make sure your file exists, try this above the ftp_put line.
if(!file_exists($file)) echo "The local file does not exist";
Edit 2 After reading down on ftp_put, it says that the remote_file does not support directories. You'll need to use ftp_chdir first.
ftp_chdir($conn_id, "tf/addons/sourcemod/configs/");
Then for remote_file, use just tf2items.weapons.txt. You can still use filepaths for the local file.
If that's a Linux server ensure that you use correct case for directory names.
"tf/addons/sourcemod/configs" is not the same as "TF/addons/sourcemod/configs";

FTP file upload using php

I need to upload a file automatically from a local computer to a remote server. I have found the following code on here:
<?php
require_once('ftp.php');
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
ftp.php is my file with the ftp authentication information. The connection works but I am getting the following error:
There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv
EDIT: I amnot sure if this makes a difference or not, but here are my $remote_file and my $file:
$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv";
What am I doing wrong here? Also, is it possible to do this if the file is on a mapped drive on my local server?
Thanks.
First thing: Try to set passive mode. You need it if you're sitting behind a firewall. (What probably is the case)
ftp_pasv($conn_id, true); // after ftp_login
Second, you have to change to dir first:
ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/');
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII);
If you want to know what's really going on, try to get error message with error_get_last() or
$php_errormsg

Categories