I'm generating a .csv file from a mysql query and the file is correctly saved on the local server where the creating query is. The next step would be to take that new file and upload it to a different FTP location (completely different server). I've tried almost everything, but I still can't get the ftp_put to work.
Here is the part of my code in question:
//Creating the file
$file = fopen(dirname(__FILE__) . "/local_export.csv","w");
$csv_line = array("item1","item2");
fputcsv($file,$csv_line);
fclose($file);
$ftp_server = "00.000.000.00";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$file = dirname(__FILE__) . "/local_export.csv";
$remote_file = "remote_export.csv";
// set up basic connection
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_pasv($conn_id, true);
// login with username and password
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";
}
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// change the current directory
if (ftp_chdir($conn_id, "transfer")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
In terms of the location of the two files, the local file is in:
http://subdomain.domain/folder/local_export.csv
While the remote file is going to go on the root / folder of the server (which is also the only one I have access to).
To me it looks like everything is in order, but still the code only generates the local file, always failing to save it to the remote location. I'm fairly new to moving files around, so I'm sorry in advance if the solution is in front of my eyes but I can't see it!
Many thanks in advance for your help.
EDIT: as pointed out in the comments below, I should've checked if the connection to the server is successful or not. I've done those checks. The connection return successful (I've also tried independently via Filezilla to make sure the account I was given is able to upload in that location and it is.). So I guess I'm back at square one.
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";
}
I have been playing around with a few php scripts trying to first at least get the php file to connect to the ftp on my distributors file and THEN i would like it to save on my file. I was going to save this as a cron to run this script daily. I can't get it to connect to the distributors ftp. Their ftp access is ftp.aphrodite.WEBSITE.net
The exact location of the file is ftp.aphrodite.WEBSITE.net/exporting/xml/products.xml
I can acces it with filezilla and my browser. This is a different language to me. Any help would be great!
<?php
$conn_id = ftp_connect("ftp.aphrodite.WEBSITE.net/exporting/xml/products.xml");
$login_result = ftp_login($conn_id, "USERNAME", "PASSWORD");
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
} else {
echo "Connected";
}
// get the file
$local = fopen("products.xml","w");
$result = ftp_fget($conn_id, $local,"httpdocs/products.xml", FTP_BINARY);
// check upload status
if (!$result) {
echo "FTP download has failed!";
} else {
echo "Downloaded";
}
// close the FTP stream
ftp_close($conn_id);
?>
If I do not use the full url to the ftp file I do not get a login when using a browser.
You supplied a wrong parameter to ftp_connect(), this function accept as parameter the hostname, not url.
$conn_id = ftp_connect("ftp.aphrodite.WEBSITE.net");
I have a web form that I will be hosting on a centOS web server. The code that I have written, will take the data that has been entered, and save it to an excel spreadsheet to a specific location. i also have a file server, this is where I would like to have the file saved to. this is the first time that i have done a server to server file save/copy/transfer. on the web server, would i have to install an ftp client to accomplish this? When testing the program locally on my computer, this is how i currently have the destination path set up.
file_put_contents('C:\Users\username\Box Sync\Driver Check In\Shortage'.
$month. '\Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
with the code being hosted on the web server, how can i change the destination path to point to my file server?
Here is a sample script of how to upload file from your localhost to any FTP server.
Source: PHP Manual
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($resource, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>
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?
What I'm trying to do is automaticly upload a zip-file through FTP using PHP, and then extract the zip-file as well. However first things first: I'm having trouble with uploading the zip-file using PHP's ftp_put function. This is my script:
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $user, $pass); //<!--same as cPanel account user and pass?
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $user";
exit;
} else {
echo "Connected to $ftp_server, for user $user";
}
// server & file info
$file = 'phpBB3.zip';
$ftp_root = '/public_html/';
$site_root = $_SERVER['DOCUMENT_ROOT'].'/scripts/';
// >>>HERE<<<
// upload the file
// >>>HERE<<<
$upload = ftp_put($conn_id, $ftp_root .$file, $site_root . $file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $file to $ftp_server as $file";
}
// unzip the uploaded file (from FTP?)
$unzip = shell_exec("unzip {$file}"); //<!-- how to do this through FTP uploaded files?
// close the FTP stream
ftp_close($conn_id);
What's happening here is that it (SOMETIMES) uploads A PART of the file ( always the same amount; like 900 kB ) and sometimes doesn't upload anything at all. It's as if the uploading process is interrupted by the rest of the script being executed while the uploading wasn't finished yet. Though I'm not sure if that's the cause of the problem.
Though, it always gives me this error:
Warning: ftp_put() [function.ftp-put]:
Connecting to port 38694 in
/home/quicksit/public_html/createacct.php
on line 93
Where the output port is always different and always larger than 20.000 ( like 30.000-50.000 ).
Could anyone help me out on this?
Thanks in advanced,
Skyfe.
There is a timeout and upload limit in the configuration file for PHP... you may want to check the setting there.