I am writing a PHP page, which stores FTP Account's address, Username and Password.
I need to validate them against the server and tell user if the credentials provided are working.
It is possible to fire system commands, System commands are preferable so that a reusable script could be written.
So can anybody tell me how do I validate the ftp credentials on bash? I am CentOS.
You can use ftp_connect() and ftp_login() :
<?php
$conn = ftp_connect($ftp_server);
$result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
if ((!$conn) || (!result)) {
echo "Failed";
} else {
echo "Success";
}
ftp_close($conn);
<?php
function testFtpCredentials($server, $username, $password){
if(!is_string($server) or !strlen($server = trim($server))){
return null;
}
if(!is_string($username) or !strlen($username = trim($username))){
return null;
}
if(!is_string($password) or !strlen($password = trim($password))){
return null;
}
if(!$connection = ftp_connect($server)){
return false;
}
$result = ftp_login($connection, $username, $password);
ftp_close($connection);
return (bool)$result;
}
// How to use it.
var_dump(testFtpCredentials('ftp.server', 'username', 'password'));
?>
A function. Use it! Don't do system calls for such easy task.
You can use this code
try {
$con = ftp_connect($server);
if (false === $con) {
throw new Exception('Unable to connect');
}
$loggedIn = ftp_login($con, $username, $password);
if (true === $loggedIn) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
print_r(ftp_nlist($con, "."));
ftp_close($con);
} catch (Exception $e) {
echo "Failure: " . $e->getMessage();
}
PHP Has an FTP Module you can use to validate the credentials
Basic example from the docs
<?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);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
Of course, if you really want to FTP on the command line via exec() or something, there are resources like these docs which will help.
A working example I just tried:
file: ftp.sh
#! /bin/bash
USER=user#domain.com
PASS=xxxxxxxxxxxx
ftp -inv domain.com <<EOF
user $USER $PASS
ls -l
file: index.php
$result = shell_exec('sh ftp.sh');
var_dump($result);
Related
Can you please assist me. I am trying to copy a file to the folder inside the application. The script is working on my local machine however when I run it on Cpanel server it shows an error "ftp_login() expects parameter 1 to be resource, Boolean given"
Here is the script which I tested
$folder_path = "192.xx.xx.xx\TMS";
$local_file = "CurrentFile\Inbound.xls";
$server_file = "CurrentFile\Inbound.xls";
//-- Connection Settings
$ftp_server = "192.xx.xx.xx"; // Address of FTP server.
$ftp_user_name = "FTP server username"; // Username
$ftp_user_pass = "FTP server Password"; // Password
$target = 'CurrentFile';
if (!file_exists($target))
{
die("Target directory doesn't exist.");
}
else if (!is_writable($target))
{
die("Insufficient privileges to write to the target directory!");
}
// 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);
// 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";
}
function fileExists($path)
{
return (#fopen($path,"r")==true);
}
ftp_close($conn_id);
I think the script is failing to connect to the FTP server.
If I add this just after I create the connection It returns "connection Failed".
if(!$conn_id) {
die("Connection failed!");
}
I am not used this but I have suggested go to this tutorial ftp_login expects parameter 1 to be a resource and let me know what happen to this
File is created on the FTP server, but its always 0 bytes large. Please give me a solution so that the file upload will working success.
I keep getting this warning:
Warning: ftp_put (): PORT command successful in C: \ xampp \ htdocs \ mailing \ teskirim-file-simpan2.php on line 30
FTP upload has failed!
My script is:
<?php
$ftp_server = "********";
$ftp_serverpath = "ftp.".$ftp_server;
$ftp_user_name = "********";
$ftp_user_pass = "***********";
$email_dir = "*******#*********";
$nyambungkeftp = ftp_connect($ftp_server);
if (false === $nyambungkeftp) {
throw new Exception('Unable to connect');
}
$loggedInnyambungkeftp =
ftp_login($nyambungkeftp, $ftp_user_name, $ftp_user_pass);
if (true === $loggedInnyambungkeftp) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
if ((!$nyambungkeftp) || (!$loggedInnyambungkeftp)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$dest = 'detectip.txt';
$source = 'C:\xampp\htdocs\persuratan\file2\detectip.txt';
echo $dest;
echo $source;
$upload = ftp_put($nyambungkeftp, $dest, $source, FTP_ASCII);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($nyambungkeftp);
?>
PHP defaults to the active FTP mode. The active mode hardly ever works these days due to ubiquitous firewalls/NATs/proxies.
You almost always need to use the passive mode.
For that call the ftp_pasv after the ftp_login:
ftp_pasv($nyambungkeftp, true);
See my article on FTP connection modes, to understand, why you typically need to use the passive mode.
Try two things:
Try FTP_BINARY instead of FTP_ASCII
Try to use passive mode doc here
I would like to a file MYFILE.csv to a remote FTP. Below is the script. The connection part works but not the file upload. I get the "There was a problem while uploading" message.
Thank you for your help.
<?php
$server = 'ftp.website.com' ;//Address of ftp server
$user_name = 'MYUSERNAME'; // Username
$password = 'MYPASSWORD'; // Password
$source_file = '/home/MYFILES.csv';
$dest = '/in/';
// set up basic connection
$connection = ftp_connect($server, 21) or die("Couldn't connect to $ftp_server");
echo "can connect";
echo "<br />";
// login with username and password
ftp_login($connection, $user_name, $password) or die("Cannot login");
echo "can login";
echo "<br />";
// upload a file
if (ftp_put($connection, $dest, $source_file, FTP_BINARY))
{ echo "successfully uploaded \n";}
else
{ echo "There was a problem while uploading \n";}
// close the connection
ftp_close($connection);
?>
Found the solution:
?php
$server = 'ftp.WEBSITE.com' ;//Address of ftp server
$user_name = 'MYUSERNAME'; // Username
$password = 'MYPASSWORD'; // Password
$source_file = '/home/MYFILE.csv';
$dest = '/in/MYFILE.csv';
// set up basic connection
$connection = ftp_connect($server, 21) or die("Couldn't connect to $ftp_server");
echo "can connect";
echo "<br />";
// login with username and password
ftp_login($connection, $user_name, $password) or die("Cannot login");
echo "can login";
echo "<br />";
// upload a file
ftp_put($connection, $dest, $source_file, FTP_ASCII) or die ("Cannot upload");
// close the connection
ftp_close($connection);
?>
i see the dest folder is "/in/".
Are you sure that its not trying to put it at the root folder of your ftp ?
(Which may belongs to root user, that's would be why it fail)
Hi I have been struggling on this for one day. My ftp connection through putty is working file where i am passing public DNS and then upload .pem key for password. But when i am trying to do so through PHP it is not able to connect. Any help would be highly appreciated.
My PHP Code is:
$server='AMAZON EC2 Public DNS';
$username='root';
$password='**i copy pasted key from .pem file**';
try {
$con = ftp_connect($server);
ftp_pasv($con, true);
if (false === $con) {
throw new Exception('Unable to connect');
}
$loggedIn = ftp_login($con, $username, $password);
if (true === $loggedIn) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
ftp_close($con);
} catch (Exception $e) {
echo "Failure: " . $e->getMessage();
}
<?php
$ftp_server = 'your_amazon_instance_url';
$ftp_user_name = 'username';
$ftp_user_pass = 'password for ftp instance for user';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name <br/>";
}
Thanks everyone for your effort. I had solved this using SSH Connection to Amazon Ec2
I want to upload a file to multiple FTP.But it is showing error :
ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115).
Have a look to my code
foreach ($channel_details as $channel_list)
{
if(isset($connection))
unset($connection);
if(isset($login))
unset($login);
if(isset($upload))
unset($upload);
$server = $channel_list['channel'];
$ftp_user_name = $channel_list['username'];
$ftp_user_pass = $channel_list['password'];
$source='file_push.html';
$dest='/public_html/'.$path.$file;
$connection = ftp_connect($server) or die("Couldn't connect to ftp server");
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($connection, true);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, $dest, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
}
Your code seems to be correct.
Maybe there is a firewall problem?
See here: https://bugs.php.net/bug.php?id=47110