PHP Function Reference

PHP ftp_cdup() Function



The PHP ftp_cdup() function is used to change the current directory to the parent directory on the FTP server.

Syntax

ftp_cdup(ftp)

Parameters

ftp Required. Specify the FTP connection to use.

Return Value

Returns true on success or false on failure.

Example:

The example below shows the usage of ftp_cdup() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";
   
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    //changing current directory to public_html
    ftp_chdir($ftp, "public_html");

    //displaying current directory
    echo "Current directory: ".ftp_pwd($ftp)."\n"; 

    //returning to the parent directory
    if (ftp_cdup($ftp)) { 
      echo "cdup successful\n";
    } else {
      echo "cdup not successful\n";
    }

    //displaying current directory
    echo "Current directory: ".ftp_pwd($ftp)."\n"; 
    
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Current directory: /public_html
cdup successful
Current directory: /
Connection closed successfully!

❮ PHP FTP Reference