PHP script to upload a file to a FTP - php

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)

Related

how to get file from ftp server and copy on own server

I want to get file from client server and copy them on my server , I have successfully connected to client server, my code is below.
// connect and login to FTP server
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxx';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "<pre>";
print_r($login);
echo "</pre>";
// get the file list for /
$filelist = ftp_rawlist($ftp_conn, "/");
// close connection
ftp_close($ftp_conn);
echo "<pre>";
print_r($filelist);
echo "</pre>";
// output $filelist
var_dump($filelist);
May anyone please advise how can I achieve this?
You can use the ftp_fget function specified here: http://php.net/manual/en/function.ftp-fget.php
(ftp_fget() retrieves remote_file from the FTP server, and writes it to the given file pointer.)
Here an example provided by the documentation:
<?php
// path to remote file
$remote_file = 'somefile.txt';
$local_file = 'localfile.txt';
// open some file to write to
$handle = fopen($local_file, 'w');
// 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 $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
echo "successfully written to $local_file\n";
} else {
echo "There was a problem while downloading $remote_file to $local_file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>
This is how I resolved this now all files will copy on your server. use ftp_ssl_connect if its secure
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxxxxx';
$ftp_conn = ftp_ssl_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn,pasv);
$output_directory="files1/ftpgetfiles/137/";
// get the file list for /
$filelist = ftp_nlist($ftp_conn, "/");
foreach ($filelist as $key => $value) {
$fp = fopen($output_directory.$value,"w");
if(ftp_fget($ftp_conn, $fp, $value, FTP_BINARY))
{
fclose($fp);
}
}
ftp_close($ftp_conn);

how to list all the directories of ftp server of iis using php for download

I set up my IIS Server in Windows and set a list of folders that should only be downloaded. Now i connected to the server using php too.
$username=$_POST["username"];
$password=$_POST["paswd"];
$host="localhost";
$ftpcon= ftp_connect($host) or die("could not connect");
$login=ftp_login($ftpcon,$username,$password);
Now what i want to list is the windows directory. That will help me navigate like the file explorer in windows. Would you mind giving me some help here
Try the below code to list all your files & directories:
<?php
$ftp_server = 'xxx.xx.xx.xx';//Replace with your IP
$conn_id = ftp_connect($ftp_server);
# login with username and password
$user = 'ftp_user_name'; //Replace with your FTP User name
$passwd = 'ftp_password'; //Replace with your FTP Password
$login_result = ftp_login($conn_id, $user, $passwd);
# check connection
if (!$conn_id)
{
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $user";
exit();
}
else
{
$cwd = ftp_pwd($conn_id);
$contentList = ftp_nlist($conn_id, "."); // This denotes the current path, you can replace this with your actual path
foreach($contentList as $contentListItem)
{
if (ftp_size($conn_id, $contentListItem) == -1)
{
#Its a direcotry
echo "Directory : ".$contentListItem."<br>";
}
else
{
#Its a file
echo "File : ".$contentListItem."<br>";
}
}
}
?>

PHP ftp connection to Amazon EC2 Instance

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

Validate FTP credentials

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);

php ftp upload problem

I am trying to write a small php function that will upload files to an FTP server and I keep getting the same error but I cannot find any fix by googling the problem, I am hoping you guys can help me here...
The error I get is: Warning: ftp_put() [function.ftp-put]: Unable to build data connection: No route to host in .
The file was created at the FTP server but it is zero bytes.
Here is the code:
<?php
$file = "test.dat";
$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";
$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}
$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($cid);
?>
I forgot to put FTP in passive mode using:
ftp_pasv($cid, true);

Categories