ftp_cdup() function in PHP

The ftp_cdup() function in PHP changes the current directory to the parent directory on an FTP server. This is equivalent to the "cd .." command in Unix/Linux systems or "cd .." in Windows command prompt.

Syntax

ftp_cdup($ftp_connection)

Parameters

The function accepts the following parameter −

  • ftp_connection − Required. The FTP connection resource returned by ftp_connect()

Return Value

The ftp_cdup() function returns TRUE on success or FALSE on failure.

Example

The following example demonstrates how to change the current directory to the parent directory −

<?php
    $ftp_server = "192.168.0.4";
    $ftp_user = "amit";
    $ftp_pass = "tywg61gh";
    
    // Establish FTP connection
    $con = ftp_connect($ftp_server);
    $res = ftp_login($con, $ftp_user, $ftp_pass);
    
    // Change to 'demo' directory
    ftp_chdir($con, 'demo');
    echo "Current directory: " . ftp_pwd($con) . "<br>";
    
    // Change to parent directory
    if (ftp_cdup($con)) {
        echo "Directory changed to parent!<br>";
    } else {
        echo "Directory change not successful!<br>";
    }
    
    echo "Current directory: " . ftp_pwd($con) . "<br>";
    
    // Close FTP connection
    ftp_close($con);
?>

The output of the above code would be −

Current directory: /demo
Directory changed to parent!
Current directory: /

Key Points

  • The function only works if you have appropriate permissions to access the parent directory

  • If already in the root directory, the function may return FALSE depending on the FTP server configuration

  • Always check the return value to handle failures gracefully

Conclusion

The ftp_cdup() function provides a simple way to navigate up one level in the directory structure of an FTP server. Always verify the operation's success using the return value before proceeding with other FTP operations.

Updated on: 2026-03-15T07:38:44+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements