error in upload file to ftp with ftp_nb_fput - php

I wrote this code for upload file via ftp.
<?php
$file = 'index.php';
$fp = fopen($file, 'r');
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Initate the upload
$ret = ftp_nb_fput($conn_id, $file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Do whatever you want
echo ".";
// Continue upload...
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "There was an error uploading the file...";
exit(1);
}
fclose($fp);
?>
and I get this error:
Warning: ftp_nb_fput(): Could not open data connection to port 2804: Connection refused
I disable my firewall but not work!

Try running in pasv mode, ftp_pasv($conn_id, true); Also please use ftp_close($conn_id) when you're done.
Thanks Ohgodwhy!

Related

ftp_nb_fput(): TYPE is now 8-bit binary with ftp_ssl_connect

Assume you have the following code:
// ...
$conn_id = ftp_ssl_connect($ftp_server);
ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
$fp = fopen($source_file, 'r');
$ret = ftp_nb_fput($conn_id, $destination_file, $fp, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// ...
$ret = ftp_nb_continue($conn_id);
// ...
}
// ...
ftp_close($conn_id);
In my setup this code produces the following warning
ftp_nb_fput(): TYPE is now 8-bit binary
If I run the same code without ftp_ssl_connect($ftp_server); but with ftp_connect($ftp_server); the code runs without a warning.
Am I using ftps with php in the wrong way or what is the problem? (PHP-Version on running system is: 7.1.13).

upload the file into the server using ftp in php

i have tried to upload the image into the server but i could not have the permission to access the folder so i have used the FTP connection. Through the android app i have encode the image and sent it to the server. In the server side, i just decode it and tried to upload it. But i could not able to upload it. Can you please help me on this.
<?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['filename'];
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$destination_file = "/upload/images/".time().jpg";
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
$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,$conn_id";
}
$upload = ftp_put($conn_id, $destination_file, $binary, FTP_BINARY);
if (!$upload) {
echo "FTP upload has failed! $upload";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($conn_id);
?>
The third param of the function ftp_put expects a filename. You are passing the image binary as a string.
Change tis line:
$upload = ftp_put($conn_id, $destination_file, $binary, FTP_BINARY);
To:
$fileName = uniqid().'.jpg';
$filePath = '/tmp/'.$uniqid;
file_put_contents($filePath, $binary);
$upload = ftp_put($conn_id, $destination_file, $filePath, FTP_BINARY);
please add ftp_pasv($conn_id, true); after successful login. Then only this will work.

Upload file through FTP using PHP

I'm curious how to upload file through FTP using PHP. Let's say I have SQL function and it will return a filename (dynamic name) and upload file using the return of filename. how can i do this?
i have try before and try in command prompt. but the error is "PHP Warning: ftp_put : failed to open stream: no such file or directory in ...."
my code :
<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");
/$query = pg_query($db, "select lookup_cell();");
$arr = pg_fetch_array($query, 0, PGSQL_NUM);
$file = $arr[0].'.csv';
$remote_file = '/nury/'; // <-- my directory on server
$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Passive mode
// ftp_pasv ($conn_id, true);
$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
//var_dump($ftp); die();
// upload a file
if ($ftp) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Remove the comment for Passive mode and then try once it will work.
<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");
/$query = pg_query($db, "select lookup_cell();");
$arr = pg_fetch_array($query, 0, PGSQL_NUM);
$file = $arr[0].'.csv';
$remote_file = '/nury/'; // <-- my directory on server
$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Passive mode
ftp_pasv ($conn_id, true);
$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
//var_dump($ftp); die();
// upload a file
if ($ftp) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>

How to upload a file to a folder location on server2, Having php script on server1

I am having a php enabled server1. I have a php code for file upload. But i need the file to be saved on server2.
I have FTP access to server2.
While searching i found this code,
<?php
$ftp_server = "199.53.23.1";
$ftp_user_name = "xxxx";
$ftp_user_pass = "**********";
$remote_dir = "http://server2/Images/";
// 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);
//default values
$file_url = "";
if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);
$file = $_FILES["uploadedfile"]["tmp_name"];
$remote_file = $_FILES["uploadedfile"]["name"];
$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}
if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
?>
It said unable to connect to Server.
Anyone have an idea on this type of requirement?
Please help.
UPDATE
Server2 doesn't support PHP
Here your line
$login_result = #ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
Remove #
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
For SFTP
$conn_id = ssh2_connect($ftp_server, 22);
ssh2_auth_password($conn_id, $ftp_user_name, $ftp_user_pass);
$sftp = ssh2_sftp($conn_id);
Uses example: $stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
*send a file
ssh2_scp_send($conn_id, '/local/filename', '/remote/filename', 0644);
*fetch file
ssh2_scp_recv($conn_id, '/remote/filename', '/local/filename');
*Create a new folder
ssh2_sftp_mkdir($sftp, '/home/username/newdir');
*Rename the folder
ssh2_sftp_rename($sftp, '/home/username/newdir', '/home/username/newnamedir');
*Remove the new folder
ssh2_sftp_rmdir($sftp, '/home/username/newnamedir');
*Create a symbolic link
ssh2_sftp_symlink($sftp, '/home/username/myfile', '/var/www/myfile');
*Remove a file
ssh2_sftp_unlink($sftp, '/home/username/myfile');

download file from ftp using php file size issue

I have to download files from linkshare server to my server by using cron.
Every thing is perfect if the file size is less then 2 GB but if exceeds it fails to download.
code is given below
$ftp_server = "***.*******.com";
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
$login_result = ftp_login($conn_id, '******', '*******');
$ret = ftp_nb_get($conn_id, $localfile, $serverfile, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Do whatever you want
echo ".";
// Continue downloading...
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "There was an error downloading the file...";
exit(1);
}
Thanks in advance
This is my code for get all files from link share ftp
<?php
session_start();
$i = $_REQUEST['i'];
if($i==""){
$i=0;
$source_dir=("linkshare");
$source_folder=dir($source_dir);
while($files_list=$source_folder->read())
{
if ($files_list!= "." && $files_list!= "..")
{
$pat="linkshare/";
unlink($pat.$files_list);
}
if($files_list!="")
{
$pat="linkshare/";
unlink($pat.$files_list);
}
}
}
$destinationnameeeeee = "linkshare/";
ini_set("max_execution_time",300000000000000000);
$ftp_server = ''; //ftp server name
$ftp_user_name = ''; //ftp user name
$ftp_user_pass = ''; //ftp user password
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$source_folder = ftp_nlist($conn_id, ".");
foreach($source_folder as $folder_list)
{
$folderlisting = explode("_",$folder_list);
$folders_list[]= $folder_list;
}
$_SESSION['folder_list'] = $folders_list;
//print_r($folders_list);
$folder_count=count($_SESSION['folder_list']);
$cur_folder = $_SESSION['folder_list'][$i];
$source_file = str_replace('.lmp', '', $_SESSION['folder_list'][$i]);
$destination_file = $destinationnameeeeee.str_replace('.lmp', '', $_SESSION['folder_list'][$i]);
echo $destination_file;
if ((!$conn_id) || (!$login_result))
{
echo "<br />FTP connection has failed!";
echo "<br />Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
}
else
{
echo "<br />Connected to $ftp_server, for user $ftp_user_name";
}
// download the file
$download = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
// check download status
if (!$download)
{
echo "<br />FTP download has failed!";
}
else
{
echo "<br />Downloaded $source_file from $ftp_server as $destination_file";
//if($i<=$folder_count)
if($i>=0)
{
$i=$i+1;
}
if($i==8)
{
exit; // 8 file only now download if you want to extent yourself
}
header("Location:ftpget.php?i=$i");
}
ftp_close($conn_id);
?>
I can load same file for after every file downloading time.
tryfollowing this link, it could help you
runtime behavior for the FTP connection

Categories