PHP ftp_put fails with "Warning: ftp_put (): PORT command successful" - php

File is created on the FTP server, but its always 0 bytes large. Please give me a solution so that the file upload will working success.
I keep getting this warning:
Warning: ftp_put (): PORT command successful in C: \ xampp \ htdocs \ mailing \ teskirim-file-simpan2.php on line 30
FTP upload has failed!
My script is:
<?php
$ftp_server = "********";
$ftp_serverpath = "ftp.".$ftp_server;
$ftp_user_name = "********";
$ftp_user_pass = "***********";
$email_dir = "*******#*********";
$nyambungkeftp = ftp_connect($ftp_server);
if (false === $nyambungkeftp) {
throw new Exception('Unable to connect');
}
$loggedInnyambungkeftp =
ftp_login($nyambungkeftp, $ftp_user_name, $ftp_user_pass);
if (true === $loggedInnyambungkeftp) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
if ((!$nyambungkeftp) || (!$loggedInnyambungkeftp)) {
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
$dest = 'detectip.txt';
$source = 'C:\xampp\htdocs\persuratan\file2\detectip.txt';
echo $dest;
echo $source;
$upload = ftp_put($nyambungkeftp, $dest, $source, FTP_ASCII);
// 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($nyambungkeftp);
?>

PHP defaults to the active FTP mode. The active mode hardly ever works these days due to ubiquitous firewalls/NATs/proxies.
You almost always need to use the passive mode.
For that call the ftp_pasv after the ftp_login:
ftp_pasv($nyambungkeftp, true);
See my article on FTP connection modes, to understand, why you typically need to use the passive mode.

Try two things:
Try FTP_BINARY instead of FTP_ASCII
Try to use passive mode doc here

Related

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

opendir() of an ftp url fails

I'm trying to open a folder from a distant server. I wrote :
if ($folderHandle = opendir($folder))
where $folder = "ftp://xxx:xxx#xxx.net:21"
I get the weird error Warning: opendir(ftp://...:21): failed to open dir: operation failed in ... on line 38
Any ideas as to where I should go from here ? Is it a problem with the FTP credentials ?
You could use PHP's FTP Capabilities to remotely connect to the server and get a directory listing:
// set up basic connection
$conn_id = ftp_connect('otherserver.example.com');
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'password');
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
}
// 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";
}
// Retrieve directory listing
$files = ftp_nlist($conn_id, '/remote_dir');
// close the FTP stream
ftp_close($conn_id);

PHP FTP upload not working

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.

php ftp upload problem

I am trying to write a small php function that will upload files to an FTP server and I keep getting the same error but I cannot find any fix by googling the problem, I am hoping you guys can help me here...
The error I get is: Warning: ftp_put() [function.ftp-put]: Unable to build data connection: No route to host in .
The file was created at the FTP server but it is zero bytes.
Here is the code:
<?php
$file = "test.dat";
$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";
$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}
$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($cid);
?>
I forgot to put FTP in passive mode using:
ftp_pasv($cid, true);

FTP Upload in PHP

I have installed XAMPP vrsion 1.7.2 on my Mac OS 10.5.7 ?
I am using the following code to upload a file , but i am getting few errors
<?
$host = 'localhost';
$usr = 'nobody';
$pwd = 'xampp';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
ftp_pasv ($conn_id, true);
// perform file upload
// file to upload:
$local_file = './del.php';
$ftp_path = '/del.php';
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
// check upload status:
if(!$upload) {
print 'Cannot upload' ;
} else {
print 'Upload complete';
}
print "\n";
/*
** Chmod the file (just as example)
*/
// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
// try to chmod the new file to 666 (writeable)
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 666\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
?>
I am getting this warning .
Warning: ftp_put() [function.ftp-put]: /del.php: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/ftp1.php on line 31
Cannot upload
Warning: ftp_chmod() [function.ftp-chmod]: /del.php: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/test/ftp1.php on line 84
could not chmod
What is the problem ? Is there any setting that I need to enable ?
It looks like PHP does not have read/write permissions to that directory. This is something that you can resolve on your Mac while developing locally but be aware that the problem may likely repeat itself when you migrate to your production server.
$local_file = './del.php';
Check your del.php file permissions.

Categories