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);
Related
In my php code below, i am trying to upload a file. I have adapted this code from one of the posts on the subject. The file i have to upload has a serial number as part of the name, which is incremented everyday. So I use the glob function to put the file name in an array and loop through the array. The script works but the file created on the server is empty. I have seen a response to my question, which is to use
ftp_pasv($conn_id, true);
But I do have this in the script, it does not work for me. I hope someone can assist me locate what the problem is.
I am working in ubuntu 18.04, also connecting to a unix machine.
As an extra question, how would I delete the local file after transfer.
Thanks for understanding.
The code is below.
#!/usr/bin/php
<?php
//define some variables
$ftp_server='server-address';
//set up basic connection
$conn_id = ftp_connect($ftp_server);
$ftp_user_name="myuser";
$ftp_user_pass="mypasswd";
$remote_path = "/";
$local_path="./";
chdir($local_path);
$local_file = glob("$local_path/OBS*");
$arrlength=count($local_file);
$cur_dir= getcwd();
echo $cur_dir . "\n";
//exit;
//login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
print_r(ftp_nlist($conn_id,"."));
for($x=0;$x<$arrlength;$x++){
//upload $local_file and save to $remote_path
if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_ASCII)){
echo "Successfully written to $remote_path)\n";
}
else {
echo "There was a problem\n";
}
}
//close the connection
ftp_close($conn_id);
?>
try like this
if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_BINARY)){
echo "Successfully written to $remote_path)\n";
}
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);
?>
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.
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);
?>
I need to write a PHP script that downloads a file (master.csv) from an ftp server (ftp.example.com) and prepares it for processing and uploading. I've tried this a number of times but my code is no good, I'm new to PHP. How would I go about doing this?
<?php
///vars
$local_file = 'xmitpart2.csv';
$server_file = 'master.csv';
$ftp_server="ftp.example.com";
$ftp_user="username";
$ftp_pass="paswd";
//connect to server
$conn_id = ftp_connect($ftp_server);
// login to ftp
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
//download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII))
{
echo "Holy Crap Finally!\n";
}
else
{
echo "Of Course.\n";
//WRONGO DIE DIE DIE
die;
}
// close the connection
ftp_close($conn_id);
You can use ftp_get();
It allows you to get a file from an FTP server and save it locally.
Once you have it saved locally you can do what ever you want to "prepare it" as you say.