Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ftp_exec() function in PHP
The ftp_exec() function is used to execute a command on the FTP server. This function is useful when you need to run server-side commands remotely through an FTP connection.
Syntax
ftp_exec(connection, command)
Parameters
connection − Required. The FTP connection resource created by
ftp_connect()command − Required. The command string to execute on the FTP server
Return Value
The ftp_exec() function returns TRUE if the command was executed successfully, otherwise FALSE on failure.
Example
The following example demonstrates how to execute a command on an FTP server ?
<?php
$ftp_server = "192.168.0.4";
$ftp_user = "amit";
$ftp_pass = "tywg61gh";
// Connect to FTP server
$connection = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
// Login to FTP server
$login = ftp_login($connection, $ftp_user, $ftp_pass);
if (!$login) {
die("FTP login failed!");
}
// Command to list files
$command = 'ls -la';
// Execute command
if (ftp_exec($connection, $command)) {
echo "Command '$command' executed successfully!";
} else {
echo "Command execution failed!";
}
// Close FTP connection
ftp_close($connection);
?>
Command 'ls -la' executed successfully!
Key Points
- The function only returns success/failure status, not the actual command output
- Not all FTP servers support command execution − it depends on server configuration
- Always establish a proper FTP connection and login before using
ftp_exec() - Common commands include file operations like
ls,pwd, orchmod
Conclusion
The ftp_exec() function provides a way to execute server-side commands through FTP connections. While it only returns execution status, it's useful for remote file management tasks when the FTP server supports command execution.
