How to convert ftp_upload.php to use ncftpput or curl instead of php

Php does support ftp uploads... this is what DA uses for file uploads to remote ftp servers.
In some cases, the errors generated by php are not sufficient to debug an ftp problem, so convert this script to use a different ftp client may help.

You can convert the:
/usr/local/directadmin/scripts/ftp_upload.php

to use curl instead of php, by first copying it to:
/usr/local/directadmin/scripts/custom/ftp_upload.php

and making the copy's contents look like:

/bin/sh
# CURL Backup Transfer
# Version 0.1a
# Copyright 2006, Sensson (www.sensson.net)
#
# This script makes it possible to transfer
# backups using your secondary uplink
# like eth1.

ETH=eth0
CURL=/usr/local/bin/curl

result=`$CURL --interface $ETH -T $ftp_local_file -u $ftp_username:$ftp_password ftp://$ftp_ip$ftp_path$ftp_remote_file 2>&1`

if grep -q -o -i "curl: (67) Access denied: 530.*$" <<< "$result"; then
echo "FTP access denied. Please check your login details."
exit 1
fi
if grep -q -o -i "curl: (6) Couldn't resolve host.*$" <<< "$result"; then
echo "Host could not be resolved. Please check your host details."
exit 1
fi
if grep -q -o -i "curl: (9) Uploaded unaligned file size.*$" <<< "$result"; then
echo "File could not be uploaded. Please check your path."
exit 1
fi
if grep -q -o -i "curl: Can't open.*$" <<< "$result"; then
echo "Can't open $ftp_local_file"
exit 1
fi

Be sure to set the ETH value appropriately to your network device.
Also, the ftp_path value must have a trailing slash to work correctly.

You can remove the --interface $ETH portion of the command if you do not need to specify any interface for curl to bind to.

Referenced from Here

The same method can be used, to convert the script to ncftpput instead of curl or php.
Edit the ftp_upload.php script and insert the following code instead:

/bin/sh
/usr/bin/ncftpput -t 25 -m -u "$ftp_username" -p "$ftp_password" "$ftp_ip" "$ftp_path" "$ftp_local_file" 2>&1
RET=$?
exit $RET



Lastly, the original method of this setup is to use php:

/usr/local/bin/php
<?

$use_pasv = true;

$ftp_server = getenv("ftp_ip");
$ftp_user_name = getenv("ftp_username");
$ftp_user_pass = getenv("ftp_password");
$ftp_remote_path = getenv("ftp_path");
$ftp_remote_file = getenv("ftp_remote_file");
$ftp_local_file = getenv("ftp_local_file");

$conn_id = ftp_connect($ftp_server);
if (!$conn_id)
{
echo "Unable to connect to $ftp_servern";
exit(1);
}

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (!$login_result)
{
echo "Inavalid login/password for $ftp_user_name on $ftp_server";
ftp_close($conn_id);
exit(2);
}

ftp_pasv($conn_id, $use_pasv);

if (!ftp_chdir($conn_id, $ftp_remote_path))
{
echo "Invalid remote path '$ftp_remote_path'";
ftp_close($conn_id);
exit(3);
}

if (ftp_put($conn_id, $ftp_remote_file, $ftp_local_file, FTP_BINARY))
{
ftp_close($conn_id);
exit(0);
}
else
{
$use_pasv = false;

ftp_pasv($conn_id, $use_pasv);

if (ftp_put($conn_id, $ftp_remote_file, $ftp_local_file, FTP_BINARY))
{
ftp_close($conn_id);
exit(0);
}
else
{
ftp_close($conn_id);
echo "Error while uploading $ftp_remote_file";
exit(4);
}
}

?>


Was this article helpful?

mood_bad Dislike 1
mood Like 0
visibility Views: 9200