Upload files to SFTP server via PHP (phpseclib) - php

I have CSVs that I want to upload to the 'incoming' folder in the SFTP server. I am using phpseclib to do this. The connection is already there but it does not output anything.
I'm not sure if what I did was correct since I haven't dealt with SFTP before. Here's what my code looks like:
$file = "leads.csv";
$server = "41.160.150.200";
//$server = "ft.bayport.co.za";
$port = "22";
$username = "";
$password = "";
//username and password removed for security reasons
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include 'Net/SFTP.php';
define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX); // or NET_SFTP_LOG_SIMPLE
$sftp = new Net_SFTP($server);
// Check SFTP Connection
if (!$sftp->login($username, $password)) {
echo 'Login Failed.';
echo $sftp->getSFTPLog();
}else{
echo 'Connected to SFTP.';
echo $sftp->pwd();
// Upload CSVs to SFTP incoming folder
echo $upload = $sftp->put("incoming/".$file, "./bayport/".$file, NET_SFTP_LOCAL_FILE);
}
I would really appreciate any help. Thanks!

Now I know what's the problem with the script.
The remote directory URL was wrong. "incoming/" should be "/incoming/"

Related

Obtaining files via ssh2_scp_recv in php

For a project i need to retreive all the files on a sftp server with a total size that amounts to about ~100 mb among all the files. I developed a script thats uses the ssh2 library and fread to get all the files, but it takes arround 10 min to get all the content. I tried ssh2_scp_recv, hoping for better performance, as shown in code bellow, but the script runs rather fast, not obtaining any file, as if the mentioned function never ran.
function get_sftp_content(){
$url = 'someurl.com';
$port = 22;
$username = "user";
$password = "password";
// Make our connection
$connection = ssh2_connect($url);
// Authenticate
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');
// Create our SFTP resource
if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create SFTP connection.');
$localDir = '/ftp_files';
$remoteDir = '/download';
// download all the files
$files = scandir('ssh2.sftp://' . $sftp . $remoteDir);
//echo '<pre>' . print_r($files, true) . '</pre>' ;
if (!empty($files)) {
foreach ($files as $file) {
ssh2_scp_recv($connection,$remoteDir.'/'.$file, $localDir.'/'.$file);
}
}
}
get_sftp_content();
Any ideas on why this happens, and is there a faster alternative to get all the files, without recurring to phpseclib?

Pureftpd-upload while uploading image through ftp

I am trying to upload image from one server to another server through FTP by using PHP.But the uploaded data shows.pureftpdupload.5809ed2f.15.7b36.24316ca6 error.
I am using this code.
$connection = 'servername';
$username = 'xxxx';
$password = 'yyyy';
$local_file = 'http://servername/test.jpg';
$remote_file = 'admin/files/company/test.jpg';
$connection = ftp_connect($server);
if (#ftp_login($connection, $username, $password)) {
// successfully connected
//echo 'connected';exit;
} else {
echo 'not connected';
return false;
}
if (ftp_put($connection, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded\n";
} else {
echo "There was a problem while uploading \n";
}
My guess is that you want to upload a local file from a web server. But by mistake or misunderstanding, you are using file's HTTP URL (http://servername/test.jpg) instead of file's local path.
Try to use a real local path, like /home/user/test.jpg.
This may work as a generic solution: $_SERVER['DOCUMENT_ROOT']."/test.jpg"

can't download file from ftp with ftp_get (no error)

I'm trying to download file from ftp, but whatever i tried i couldn't download any file from ftp and it is not giving any error.
My code fetching file names from database and compares these file names with existing ftp file(names). If file name exist on ftp i want to download this file to my local-destination folder.
I can read ftp file names but i can't download them. Hope you can help me.
Here is my code
error_reporting(E_ALL);
$ftp_server = "xx.xx.y12.34"; //server
$bt = ftp_connect($ftp_server); // connect to ftp
$username = "myusername"; $pass = "mypass"; //username and password
$connection = ftp_login($bt, $username, $pass); // login ftp with username and password
ftp_pasv($bt, true);
if ((!$bt) || (!$connection)) {
echo "failed";
exit;
}else {
$path = "/1_Swatch Files/LG Swatches/";
$destination_folder = "/home/faruk/lasenza/media/color_samples/";
// mysql connection codes..
$results = $readConnection->fetchAll($query); // fetch file names from database
$contents_on_server = ftp_nlist($bt, $path);
foreach($results as $result){
$check_file_exist = $result["CLR_CODE"].".gif";
if(in_array($check_file_exist, $contents_on_server)) {
$server_file = $result["CLR_CODE"].".gif";
if (ftp_get($bt, $destination_folder , $server_file, FTP_BINARY)) {
echo 'Succeeded';
}else{
echo "There was a problem";
}
break;
}
}
}

FTP Files are not accessible using fopen or file_exists or file_get_contents

I am trying ftp connections for file read.
All I want to do is
read particular ftp dir
list all its text files
and read them one by one
I am successful in listing files in given directory.
For eg: below code returns $emailfiles array with TEST.txt as one value.
But when I try to open/read the same file content using fopen/file_get_contents , it is giving the error: " Failed to open Stream "
I am able to access a file from another ftp : .38, which is public ftp.
The ftp I want to actually use is .94, for which I have full privileges, as I am able to access files from browser with same link. I'm also able to write/paste files from windows explorer.
But I am unable to read the same file from php code.
Below is my code for ftp connection and testing.
$ftp_server = "10.96.5.94";
$ftp_serverpath = "ftp://".$ftp_server;
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$email_dir = "ebill";
try {
$con = ftp_connect($ftp_server);
if (false === $con) {
throw new Exception('Unable to connect');
}
$loggedIn = ftp_login($con, $ftp_user_name, $ftp_user_pass);
if (true === $loggedIn) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
ftp_pasv($con, true);
$emailfiles = ftp_nlist($con, $email_dir);
print_r($emailfiles);
//$filename = "ftp://10.16.0.38/ebill/TEST.txt";
$filename = "ftp://10.96.5.94/ebill/TEST.txt";
$pr = file_exists($filename);
// OR
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
Any help is greatly appreciated, Thank you.
Ok, This solved my error.
$filename = "ftp://username:pa‌​ssword#hostname/path/to/file";
Thanks.

PHP File Transfer Script .. ALMOST DONE!

Can someone figure out what this is not working? I kept trying different things, double checking syntax etc but I get the error message I have setup for when a file does not transfer. This is my first PHP script.
I appreciate any help I will do research in the meantime.
Using NetBeans
<?php
$user = "username";
$password = "password";
$server_file = 'mag_hounddog.mp3';
$local_file = 'C:\users\kazuiman\desktop\mag_hounddog.mp3';
$ftp_server = "ftp.mysite.com";
// setup a basic connection to the ftp server
$connectionID = ftp_connect($ftp_server) or die("Connection was Not Successful");
// login with username and password for your account
$loginResult = ftp_login($connectionID, $user, $password);
echo ftp_pwd($connectionID);
// changes the directory to the tvstation you can hard code one in here.
if (ftp_chdir($connectionID, "test1nbiiwcdp_apps/tv2beta/streams/_definst_/")) {
echo "Current Directory is Now..." . ftp_pwd($connectionID) . "\n";
} else {
echo "Error Changing Directory .. perhaps it doesn't exist?";
}
// Now to download the files and put them onto your local machine
if (ftp_get($connectionID, $local_file, $remote_file, FTP_BINARY)) {
echo "Succesfully written $server_file to $local_file\n";
} else {
echo "There was a problem with the FTP transfer please check script\n";
}
// close the connection
ftp_close($connectionID)
?>
You have never defined the variable $remote_file.
In your preamble it is called $server_file.
Is this a copy-paste-error?

Categories