如何使用 PHP 在 FTP 上复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4853269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:05:32  来源:igfitidea点击:

How To Copy Files Around FTP Using PHP

phpftp

提问by Rickstar

I am tring to copy a file from one folder to another using the php ftp functions.

我正在尝试使用 php ftp 函数将文件从一个文件夹复制到另一个文件夹。

e.g

例如

Copy This File:httpdocs/user_images/Services/File 1.jpg

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

To:httpdocs/user_images/folder11

至:httpdocs/user_images/folder11

i have tried to use ftp_fput but i am not have any luck with it.

我曾尝试使用 ftp_fput 但我没有任何运气。

回答by Mark Kennedy

Perhaps a little known fact: the copy() function in PHP can be used to copy files to an FTP server, though without as much control as you get by using the ftp-specific functions.

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

In other words, this can do the job:

换句话说,这可以完成这项工作:

if(copy('local/file.img', 'ftp://user:[email protected]/remote/dir/file.img')) {
  echo "It worked!!!";
}

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

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

回答by Merijn

From the manual page on ftp_puton PHP.net:

从手册页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 ; 
} 
?>

回答by rockerest

Unless you're actually moving files between servers or to somewhere that PHP doesn't have access, use copy()(php)

除非您实际上是在服务器之间或在 PHP 无法访问的地方移动文件,否则请使用copy()(php)

<?
copy('httpdocs/user_images/Services/File 1.jpg', 'httpdocs/user_images/folder11/File 1.jpg');
?>

回答by Niraj_Palange

the Copy Function will not work. You need to use ftp_get () and ftp_put() functions in order to achieve this task

复印功能不起作用。你需要使用 ftp_get() 和 ftp_put() 函数来完成这个任务