Reputation: 87
I am unable to connect to a FTP server when i am connecting it through our PHP code. Most embarrassing thing is that i get only false when trying to connect server with ftp_connect() method. No errors are reported. This is my first time when i am working with FTP in PHP. if anyone who have previously worked on it can help me out here. Here is my code :
/* Source File Name and Path */
$remote_file = $_SERVER['DOCUMENT_ROOT'] ."/some-folder/file.txt";
/* New file name and path for this file */
$ftp_host = 'ftp://targetserver.com/some folder/';
$ftp_user_name = 'user';
$ftp_user_pass = 'XXXXX';
/* New file name and path for this file */
$local_file = 'ftp://targetserver.com/some-folder/file.txt';
/* Connect using basic FTP */
$connect_it = ftp_connect( $ftp_host, 21);
var_dump($connect_it);
/* Login to FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
/* Download $remote_file and save to $local_file */
if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
/* Close the connection */
ftp_close( $connect_it );
Upvotes: 0
Views: 5608
Reputation: 7
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
<?php
$ftp_server = "94.23.x.xxx";
$ftp_username = "anxxxxx";
$ftp_password = "6xxxxxxxx";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
// login
if (@ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "conectd as $ftp_username@$ftp_server\n";
}
else
{
echo "could not connect as $ftp_username\n";
}
$file = $_FILES["uploadedfile"]["name"];
$remote_file_path = "/JustForTest".$file;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
Then what's the problem with the above code?
Upvotes: 0
Reputation: 5074
Actually getting false means that your ftp client couldn't connect to the server for some reason
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
The best to do before you dive into php debugging is to try out another ftp client such as https://filezilla-project.org/ and see if all works fine, if not you will have saved your time because the issue not coming from your php script.
UPDATE 1
Then try this:
$ftp_host = 'targetserver.com';
http://php.net/manual/en/function.ftp-connect.php#refsect1-function.ftp-connect-parameters
The FTP server address. This parameter shouldn't have any trailing slashes and shouldn't be prefixed with ftp://.
And as for local_file and remote_file, they must be paths and not urls http://php.net/manual/en/function.ftp-get.php#refsect1-function.ftp-get-examples
Upvotes: 3
Reputation: 223
As ftp_connect function doesn't produce an error, I assume the extension is installed. It is very possible this could be a firewall issue with port 21 blocked. (This is a very common reason)
Upvotes: 0