Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland asked on

PHP: FTP a file from a subfolder on IIS to a server not on port 21

Hi All,

I'm looking to FTP a file using PHP with the following...

  • the file is in a sub folder
  • the ftp is not on port 21
  • the PHP server is running on IIS

All the samples online are missing either the port or subfolder.

Any help would be greatly appreciated.


Avatar of undefined
Last Comment

8/22/2022 - Mon
Scott Fell

What scripts have you found so far? And what error did you get?  Changing the port is easy enough. Instead of a FTP, do you mean to put a file like a file upload?  That would be more common.

There is something in the manual, https://www.php.net/manual/en/book.ftp.php you would need to have FTP on for the server as well as proper permissions.
Dave Baldwin

With FTP, you can not login directly to a subfolder.  You have to login to the server and use FTP commands to change to the folder you want to access.  From https://www.php.net/manual/en/ref.ftp.php :


David Favor

1) The file is in a sub folder

As @Dave Baldwin mentioned, you'll connect to the server first, then cd to the directory/folder of interest.

2) The ftp is not on port 21

Then you'll connect to the appropriate port.

Tip: FTPd is a security travesty... so most people run SFTPd on port 22.


So if the server where you're connecting is running SFTPd, then you'll connect to port 22 + use SFTP code (not FTP code) to connect + do your transfer.

3) The PHP server is running on IIS.


So this has no bearing on your question.

Skip all HTTPS/PHP issues, as you'll be connecting between FTPd (server) + FTP (client) with no HTTPS or PHP involvement.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Scott Fell

Hi Detox, I think it will help us to know exactly what you are doing. As noted, FTP may not be what you are looking for.

Are you trying to move a file from a users machine to your server? or you are trying to go from server to server?  
Doing a file upload can work in the same manner but you have more control and it can move over https

Here is the code snippet I am using


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// connect and login to FTP server
$server_filename = "myfile.csv";

// Connect to FTP server
// Server name
$ftp_server = "ftp.mysite.co.uk";
// Use correct ftp username
$ftp_username = "myuser";
// Use correct ftp password corresponding
// to the ftp username
// File name or path to upload to ftp server
$file = "myfile.csv";
// Establishing ftp connection
$ftp_connection = ftp_connect($ftp_server, 999)
    or die("Could not connect to $ftp_server");
if( $ftp_connection ) {
    echo "Successfully connected to the ftp server!";
    // Logging in to established connection with
    // ftp username password
    $login = ftp_login($ftp_connection,
                $ftp_username, $ftp_userpass);
    if( $login ) {
        // Checking whether logged in successfully or not
        echo "<br>logged in successfully!";
        if (ftp_put($ftp_connection,
            $server_filename, $file, FTP_ASCII))
          echo "<br>Uploaded successful $file.";
        else {
          echo "<br>Error while uploading $file.";
    else {
        echo "<br>login failed!";
    // Echo ftp_get_option($ftp_connection, 1);
    // Closing  connection
    if(ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";

Open in new window

This is the error it returns (line 28 in bold above)

Fatal error: Uncaught Error: Call to undefined function ftp_connect() in C:\inetpub\wwwroot\ftp.php:28 Stack trace: #0 {main} thrown in C:\inetpub\wwwroot\ftp.php on line 28 

Open in new window


@David Favor I thought it was worth mentioning IIS as the file path might have been different, and an adjustment needed to the PHP8 .ini file.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

sorted it.... i needed to add the dll to php.ini


Open in new window


This is the updated script.... so i still need to copy the file across to the correct folder


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// connect and login to FTP server
$ftp_server = "";
$ftp_username = "user";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server,2221) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

// then do something...

// close connection


Open in new window


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question