Skip to content
Advertisement

php7.3 ftp_cdup() throws “CDUP successful” exception in some FTP servers [closed]

for($i=1;$i<=$path_div;$i++)         
{
    try{
        $res = ftp_cdup($this->conn);
        if(!$res){
            throw new Exception('ftp_cdup fail');
        }
    }catch (Exception $e){
        /**
         * :ftp_cdup(): CDUP successful. "/" is current directory
         */
        if(strpos($e->getMessage(),'ftp_cdup(): CDUP successful')>=0){
            //normal
        }else{
            throw new Exception('ftp_cdupfail:'.$e->getMessage());
        }
    }
}

Exception is

ftp_cdup(): CDUP successful. “/” is current directory

In fact this is normal.

Advertisement

Answer

First, I do not think that ftp_cdup (nor any other PHP FTP function) throws any exception. You probably have some error handler in place that converts PHP warnings into exceptions, don’t you?


Anyway, PHP expects that the server responds with code 250 to a successful CDUP command. Your server probably responds with some other 2xx success code (like plain 200). I do not think that PHP is right here. And I do not think you can really do anything about it.

Instead of using ftp_cdup, you can do:

$resp = ftp_raw("CDUP");

And check the $resp for the 2xx code.

Or just use ftp_chdir instead of ftp_cdup.


I do not think that FTP RFC 959 really mandates that the response must be 200. It’s just an example. The problem probably comes from the inconsistency in the examples for CWD and CDUP. I’d expect both to use the same code. But the RFC shows 250 for CWD, while 200 for CDUP. Even the RFC says that: “The reply codes for CDUP be identical to the reply codes of CWD.”

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement