如何使用PHP在FTP周围复制文件

Ric*_*tar 3 php ftp

我想使用php ftp函数将文件从一个文件夹复制到另一个文件夹.

例如

复制此文件: httpdocs/user_images/Services/File 1.jpg

要: httpdocs/user_images/folder11

我曾尝试使用ftp_fput,但我没有运气.

Mar*_*edy 9

也许一个鲜为人知的事实:PHP中的copy()函数可用于将文件复制到FTP服务器,尽管没有使用特定于ftp的函数所获得的控制.

换句话说,这可以做到这一点:

if(copy('local/file.img', 'ftp://user:password@ftp.example.com/remote/dir/file.img')) {
  echo "It worked!!!";
}
Run Code Online (Sandbox Code Playgroud)

http://php.net/manual/en/function.copy.php


Mer*_*ijn 7

从手册页ftp_putPHP.net:

<?php 
// bool ftp_copy  ( resource $ftp_stream  , string $initialpath, string $newpath, string $imagename ) 
function ftp_copy($conn_distant , $pathftp , $pathftpimg ,$img){ 
        // on recupere l'image puis on la repose dans le nouveau folder 
        if(ftp_get($conn_distant, TEMPFOLDER.$img, $pathftp.'/'.$img ,FTP_BINARY)){ 
                if(ftp_put($conn_distant, $pathftpimg.'/'.$img ,TEMPFOLDER.$img , FTP_BINARY)){ 
                        unlink(TEMPFOLDER.$img) ;                                              
                } else{                                
                        return false; 
                } 

        }else{ 
                return false ; 
        } 
        return true ; 
} 
?>
Run Code Online (Sandbox Code Playgroud)