PHP: Hypertext Preprocessor (original) (raw)
ftp_chdir
(PHP 4, PHP 5, PHP 7, PHP 8)
ftp_chdir — Changes the current directory on a FTP server
Description
Parameters
ftp
An FTP\Connection instance.
directory
The target directory.
Return Values
Returns [true](reserved.constants.php#constant.true)
on success or [false](reserved.constants.php#constant.false)
on failure. If changing directory fails, PHP will also throw a warning.
Changelog
Version | Description |
---|---|
8.1.0 | The ftp parameter expects an FTP\Connection instance now; previously, a resource was expected. |
Examples
Example #1 ftp_chdir() example
`<?php// set up basic connection ftp=ftpconnect(ftp = ftp_connect(ftp=ftpconnect(ftp_server); // login with username and password loginresult=ftplogin(login_result = ftp_login(loginresult=ftplogin(ftp, ftpusername,ftp_user_name, ftpusername,ftp_user_pass); // check connection
if ((!$ftp) || (!$login_result)) {
die("FTP connection has failed !");
}
echo
"Current directory: " . ftp_pwd($ftp) . "\n";// try to change the directory to somedir
if (ftp_chdir($ftp, "somedir")) {
echo "Current directory is now: " . ftp_pwd($ftp) . "\n";
} else {
echo "Couldn't change directory\n";
}// close the connection
ftp_close($ftp);
?>`
See Also
- ftp_cdup() - Changes to the parent directory
- ftp_pwd() - Returns the current directory name
Found A Problem?
17 years ago
`Thanks to h3 at valleyfield dot net
Same function with some minor changes and comments added
FTP function checks if a directory exists
originaldirectory=ftppwd(original_directory = ftp_pwd( originaldirectory=ftppwd(ftpcon ); // test if you can change directory to $dir // suppress errors in case $dir is not a file or not a directory if ( @ftp_chdir( ftpcon,ftpcon, ftpcon,dir ) ) { // If it is a directory, then change the directory back to the original directory ftp_chdir( ftpcon,ftpcon, ftpcon,original_directory ); return true; } else { return false; } } ?>`
info at swiftyexpress dot com ¶
16 years ago
`Works like the other functions in this page's notes, but this one doesn't make use of a global FTP connection, so it takes parameters like the other functions in the extension
origin=ftppwd(origin = ftp_pwd(origin=ftppwd(ftp);// Attempt to change directory, suppress errors if (@ftp_chdir($ftp, $dir)) { // If the directory exists, set back to origin ftp_chdir($ftp, $origin); return true; }// Directory does not exist return false; } ?>[NOTE BY danbrown AT php DOT net: As the contributor mentions, the original function was noted here by (h3 AT valleyfield DOT net) on 13-JUL-2007.]
`