php upload local file to ftp server - php

i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size!
also i tried to enable passive mode but it still uploads an empty file.

Try enabling track_errors and access $php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);

I had the same issue. When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected.

I had ran into this as well. Bit late but thought I'd post my solution:
$file_name = "localfile.txt";
Get content from your existing file
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content
$content = "This is content";
upload file
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);

Related

Ho to use ftp_put for file upload in PHP?

I try to upload a file using ftp_put after submitting a form. However my code does not work as expected:
$ftpHost = '192.168.180.36';
$ftpUsername = 'userdownload';
$ftpPassword = 'Toms!';
$filepath = "C://xampp/htdocs/Helpdesk/fmt/download";
$connId = ftp_connect($ftpHost,21) or die("Couldn't connect to $ftpHost");
$ftpLogin = ftp_login($connId, $ftpUsername, $ftpPassword);
$file_name = $_FILES['file']['name'][$i];
$tmp_name = $_FILES['file']['tmp_name'][$i];
// try to upload file
if(ftp_put($connId, $file_name, $filepath.'/'.$file_name, FTP_BINARY)){
echo "File transfer successful - $file_name";
}else{
echo "There was an error while uploading $file_name";
}
And I get this error message:
There was an error while uploading $file_name
There are two thing i noticed in your code. the first one is the mode of transfer depends upon your file type.Files that are in the ascii/text file extension list are transferred as ascii, all other files are transferred as binary.The second one is may be there is a problem in your file which you received may be it is black first check that. You can try this code it is working for me.
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>

PHP FTP Upload function

i have this function in PHP:
function UploadFileToFTP($local_path, $remote_path, $file, $filename) {
global $settings;
$remote_path = 'public_html/'.$remote_path;
$ftp_server = $settings["IntegraFTP_H"];
$ftp_user_name = $settings["IntegraFTP_U"];
$ftp_user_pass = $settings["IntegraFTP_P"];
//first save the file locally
file_put_contents($local_path.$filename, $file);
//login
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
}
//change directory
ftp_chdir($conn_id, $remote_path);
$upload = ftp_put($conn_id, $filename, $local_path.$filename, FTP_BINARY);
// check upload status
if(!$upload) {
echo "FTP upload has failed!";
}
// close the FTP stream
ftp_close($conn_id);
}
i call it here:
UploadFileToFTP('p/website/uploaded_media/', 'media/', $_FILES["file"]["tmp_name"], $filename);
the selected file is being moved into the local directory and also being uploaded to FTP however the file is becoming corrupt because it is not being uploaded correctly.
how can i get the file uploading properly?
Depending on what kind of file you're moving you may need to switch from FTP_BINARY to FTP_ASCII
http://forums.devshed.com/ftp-help-113/ftp_ascii-ftp_binary-59975.html
When uploading a file to PHP it stores the uploaded file in a temporary location, the location is stored in $_FILES["file"]["tmp_name"].
You are then passing that value into your UploadToFTP function as the variable $file.
Then you try to save a copy of the uploaded file:
//first save the file locally
file_put_contents($local_path.$filename, $file);
What this will do is write the string contained within $file (i.e. the path of the temp file) to your new location - but you want to write the content of the file.
Instead of using file_put_contents use move_uploaded_file:
move_uploaded_file($file, $local_path.$filename);

How to upload file to FTP using cron jobs

I want to be able to upload a csv file once a day (locally from my pc) to ftp.
I then am going to insert this csv file into a mysql table.
I've created the cron job to pick up a csv and insert it into the database, but I'm struggling how to figure out how to pick up a file that is on my loacl pc and upload it to FTP.
Has anyone got any ideas?
Thanks
Adi
You can do using the ftp extension in PHP, something like:
$conn = ftp_connect("destination.host", 21) or die("failed to connect");
ftp_login($conn, $user, $pass) or die("failed to login");
ftp_put($conn, "/path/on/ftp/server", "/path/on/your/local", FTP_BINARY) or die("failed to upload);
More details: http://us2.php.net/manual/en/book.ftp.php
// open some file for reading
$file = 'somefile.txt';
$fp = fopen($file, 'r');
$ftp_user_name="xxxxxx";
$ftp_user_pass="xxxxx";
// set up basic connection
//$conn_id = ftp_connect($ftp_server);
$conn_id = ftp_connect("xxxxxxx.com", 21) or die("failed to connect");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);

How can I open a file from another ftp and write?

I tried to open a file in another ftp and write but I coulnd how can I do that?
$ftpstream = #ftp_connect('****');
//Login to the FTP server
$login = #ftp_login($ftpstream, '****', '***');
if($login) {
echo "Conectado";
$fp = fopen('file.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
}
The code you have posted is wrong. It opens up an FTP stream (ftp_connect), but then writes a file to the local file system (fopen). Your ftp stream won't allow you to write to it with fwrite- you need to use the commands to transfer entire files.
You can do what you want with fopen if you use an ftp:// scheme.
For example:
$fp = fopen("ftp://user:password#example.com/file.txt","w");
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
Alternativley, you can write the file to the local file system and use an ftp stream to transfer it:
$file = 'somefile.txt';
// create your file in the local file system here.
$remote_file = 'readme.txt';
// 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);
As always, be clear on what you want to do.
PHP FTP reference is here
PHP scheme & wrapper reference (for use with fopen) is here

Moving an uploaded file onto a remote server

I am trying to move an uploaded file onto a remote server, this isn't working;
move_uploaded_file($tmp_name, "uploads/$code1/$code.$fileex");
$ftp_server = "IP";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$file = $tmp_name;
$remote_file = "/public_html/test/uploads/";
// 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);
I get this erorr;
Warning: ftp_put() [function.ftp-put]: Can't open that file: Is a directory in /home/file/public_html/uploaded.php on line 52
Your $remote_file variable is pointing to a directory when it should point to a file. Try changing $remote_file to $remote_file = "/public_html/test/uploads/".$file;
You should probably wrap the portion that uploads the file in an if statement that checks to see if you are actually connected properly to the FTP
Also, when uploading a file, you need file 1 and file 2. Right now you've supplied file 2 and a directory.
http://php.net/manual/en/function.ftp-put.php
The file you are trying to move to is the directory "/public_html/test/uploads/", you need to append the filename and extension onto the directory.
Add the following line at the end of the /etc/vsftpd.conf file
Add pasv_promiscuous=YES it

Categories