How to overwrite existing folder or file in php ft

2019-06-14 15:59发布

how to overwrite folder/file if exist via php ftp using ftp_put . the default is not overwriting the files.

 function ftp_putAll($conn_id, $folder, $remotedir) {                            // Called from moveFolder function at line 161 //
$d = dir($folder);
while($file = $d->read()) {                                                             // do this for each file in the directory
    if ($file != "." && $file != "..") {                                                // to prevent an infinite loop
        if (is_dir($folder."/".$file)) {                                                // do the following if it is a directory
            if (!@ftp_chdir($conn_id, $remotedir."/".$file)) {
                ftp_mkdir($conn_id, $remotedir."/".$file);                              // create directories that do not yet exist
            }
            $stream_options = array('ftp' => array('overwrite' => true));
            $this->ftp_putAll($conn_id, $folder."/".$file, $remotedir."/".$file);       // recursive part
        } else {
            if(ftp_put($conn_id, $remotedir."/".$file, $folder."/".$file, FTP_ASCII)) { 
                $upload = ftp_put($conn_id, $remotedir."/".$file, $folder."/".$file, FTP_ASCII);
                }

else {

} }

2条回答
啃猪蹄的小仙女
2楼-- · 2019-06-14 16:11

It would depend on the implementation of the FTP server. If the file overwrite is not allowed, first delete the file before uploading.

查看更多
相关推荐>>
3楼-- · 2019-06-14 16:21
   function ftp_putAll($conn_id, $src_dir, $dst_dir){
  $d = dir($src_dir);

    while($file = $d->read()) { // do this for each file in the directory

        if ($file != "." && $file != "..") { // to prevent an infinite loop

            if (is_dir($src_dir."/".$file)) { // do the following if it is a directory

                if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {

                    ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist                                                
                }


                ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part

            } else {


                @ftp_put( $conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY);
            }
        }
    }
    $d->close();
}

can you please try this above code.

Following are function parameter

connection id , source path , destination path

查看更多
登录 后发表回答