cronjob to copy a file to our server via FTP - php

Is it possible for a cron job to copy a file from a remote server and move it to our server and over-right the file currently sitting there?
I have looked for an answer on here and not found a suitable solution as most are moving a file to and not from a remote server
there will be two sets of ftp details to include.
This is for a product feed and i really cant get my head around it.
Am i on the right line of thinking with this that i have adapted.
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// 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);
// upload a file
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";
}
// close the connection
ftp_close($conn_id);
?>

Yes, it's possible.
Cronjobs are made to execute scripts periodically (every hours, every day, etc...).
So everything you can do with a script, you can do with cronjobs.

after playing around with the code i noticed i was using ftp_put and not ftp_get
here is the working code
<?php
$file = 'remotefile.txt';
$remote_file = 'ourfile.txt';
$ftp_server ='example.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
// 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);
// upload a file
if (ftp_get($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>

Related

send file with post, then upload with ftp

This is a very basic question since I do not properly understand the underlying concepts of the different protocols. Please be indulgent.
I can upload files to my site with FTP using Filezilla. Now I'm developing a user interface for sending data to update a database and files (photos) to be saved in a directory. I send this data with POST (see here). I get the file, but cannot save it to the destination directory performing
move_uploaded_file($_FILES['PhotoToUpload']['tmp_name'],"../../Photos/".$file_name);
I think is because of permissions (currently 755). Since I can currently upload with Filezilla FTP (so I understand I have the permissions to do that),
my question is:
Is bad practice (or conceptually wrong) to take the temp file created after the POST and load it with a FTP code like the following (taken from here)?
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "../../Photos/";
$source_file = $_FILES['PhotoToUpload']['tmp_name'];
// 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";
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);
?>

Copy a file from FTP server to Cpanel using Php

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

Unable to send file from ftp to another ftp

I have searched a lot for the answer and found some results but none of one is working for me. I want to transfer zip file from one server to another server using ftp functions. File went transferred but without data. Here is my code as follows:
<?php
$file = 'LICENSE.zip';
$fp = 'LICENSE.zip';
$ftp_server = "ftpserver name";
$ftp_user_name = "ftp username";
$ftp_user_pass = "ftp password";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// login with username and password
// upload a file
ftp_chdir($conn_id,'/dev');
if (ftp_put($conn_id, $file, $fp, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
fclose($fp);
?>
Even I am unable to transfer a simple .php or .txt file with their data. In this case only files without data is moving.

PHP Form post to TXT file on remote FTP

Here is the script that writes form data to txt file on same server (Linux). On every post it generates new file.
$myfile='/home/mysite/public_html/nar/fis_'.date('D_Mi').'_'.date('dmY_Hi').'.txt';
$fh=fopen($myfile,"w");
# Now UTF-8 - Add byte order mark
fwrite($fh, pack("CCC",0xef,0xbb,0xbf));
fwrite($fh,$upisufajl);
fclose($fh);
But now I need it to write on remote FTP server with username and password that is on Windows.
I have address: ftp://89.142.185.206/new_files/ and username and password.
What do I need to do? Examples would be appreciated.
Thanks guys
Since i assume that you have successfully written content to your text file, make use of this below script to login to FTP Server to upload your text file.
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//your textfile
$remote_file = "remfile.txt";
$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";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
ftp_close($conn_id);

PHP FTP : Access Denied

I was working on with a FTP upload from my php page.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();
$ftp_server = "myserver";
$ftp_user_name="myuser";
$ftp_user_pass="mypass";
$remote_file="myfile.txt";
$file="myfile.txt";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $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";
}
// close the connection
ftp_close($conn_id);
?>
It gives me a Warning in browser like
Warning: ftp_put(): Access is denied. in /var/www/html/ftpcheck.php on line 17. There was a problem while uploading myfile.txt.
I checked the access permissions on the file. But its accessible. Can any one tell me why this happens?
Most probably this is a permission issue. When you are uploading a file via FTP, you also need to check the directory's permission. When you're saying it's accessible, it doesn't mean it's writable.
You don't check the result of the login operation:
if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
You should also try a manual FTP operation from the PHP host to the FTP host to ensure that you can log-on with these credentials and put a file. This will help you establish whether it's your code at fault or the FTP credentials.
<?php
class Ftp {
function upload()
{
$ftp_server="50.56.113.39";
$ftp_user_name="******";
$ftp_user_pass="***";
$file = "form.pdf";//tobe uploaded
$remote_file = "uploads1/test.pdf";
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY))
{
echo "successfully uploaded $file\n";
//exit;
}
else
{
echo "There was a problem while uploading $file\n";
//exit;
}
// close the connection
ftp_close($conn_id);
}
}
?>

Categories