Upload file through FTP using PHP - 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);
?>

Related

Create and upload a new file via ftp in php [duplicate]

This question already has answers here:
PHP ftp_put fails
(2 answers)
Closed 1 year ago.
Is it possible to upload a new file in ftp instead of rewrite an old one? I need code for creating a new file and uploading it into the server via ftp. If I try to upload it via ftp_put(), it does not work.
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
$ftp = ftp_connect("web");
ftp_login($ftp, "user", "pass");
echo ftp_put($ftp, "Tulassi/tulasi_test", $objPHPExcel, FTP_BINARY);
ftp_close($ftp);
exit;
<?php
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = '';
$destination_file = "new.xls";
$source_file = $objWriter;
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// 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";
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII);
if (!$upload) {
echo "FTP upload has failed!112";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>

File upload via FTP error

Code:
$file = '/export/clients.xml';
$remote_file = '/clients.xml';
$ftp_server = "ftp://my.address.com";
$ftp_user_name = "user";
$ftp_user_pass = "password";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
Now the original file path is: ftp://my#address.com/domains/website.com/public_html/export/clients.xml
How should I write the file path so that it could complete the file transfer? I want the file to be copied into the root directory of my FTP server. But it doesn't work this way.
Problem was FTP server name. I removed the ftp:// part and now it's working.

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.

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

ftp_get will not download image

$fp = 'test.png';
$server_file = "dir/testing.png";
//-- Connection Settings
$ftp_server = 'ftp.net';; // Address of FTP server.
$ftp_user_name ='user'; // Username
$ftp_user_pass = 'password'; // Password
// set up basic connection
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$mode = ftp_pasv($conn_id, TRUE);
$download=ftp_get($conn_id, $fp, $server_file, FTP_BINARY);
// try to download $server_file and save to $local_file
if ( $download) {
echo "Successfully $fp\n";
} else {
echo "There was a problem\n";
}
ftp_close($conn_id);
When I run this it says it is successfully, but will not write or download the file.
If I echo file_get_contents($fp); then it will display the image deconstructed into text [in the Web browser]
In need to download images and video...
Try adding
header('Content-Type: image/png');

Categories