简体   繁体   中英

Execute PHP script on FTP server connected to in another local PHP script

is it possible to execute a php script on the server when connecting it. (not on the server, which builds the connection)?

<?php
// set up basic connection
$ftp_server = "localhost";
$conn_id = ftp_ssl_connect($ftp_server);

// login with username and password
$ftp_user_name = "myuser";
$ftp_user_pass = "mypass";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name";
        exit;
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }

$buff = ftp_rawlist($conn_id, '../ftpdest/index.php?action=download_now');
var_dump($buff);
ftp_close($conn_id);

Some , but not many, FTP servers support SITE EXEC command that allows you to execute arbitrary or selected shell commands on the server. The command can be remote php binary.

In PHP, you can use ftp_site function for that:

ftp_site($conn_id, "EXEC php -r \"echo 'Hello world!';\"");

But this will probably not work with your server. In most cases, you won't be able to execute a shell command over FTP.


If your FTP server is actually a web server, you can use FTP to upload a PHP script and then use the web server to execute the script.

ftp_put($conn_id, "/httpdocs/script.php", "/local/path/script.php");
file_get_contents("https://example.com/script.php");

If you have an SSH/shell access, use the SSH to execute your PHP script/code. The SSH is an API designed for executing script/code (contrary to FTP).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM