用于将文件从1个FTP复制到另一个FTP的PHP脚本

时间:2015-08-11 23:32:55

标签: php ftp copy

我想将文件(例如MYFILE.csv)从 FTP'SOURCE'复制到 FTP'TARGET'。您建议在php 脚本中使用什么类型的命令?

我试过这个但是没有用。

<?php
$server = 'ftp.TARGET.com' ;//address of ftp server
$user_name = 'USER_TARGET'; // Username
$password = 'PASSWORD_TARGET'; // Password

$source = 'MYFILE.csv'; 
$dest = '/in/MYFILE.csv';
$mode='FTP_ASCII'; 

// set up basic connection
$connection = ftp_connect($server) ;

// login with username and password
$login_result = ftp_login($server, $user_name, $password);

// upload a file
if (ftp_put($connection, $dest, $source, $mode)) {
 echo "successfully uploaded $source\n";
} else {
 echo "There was a problem while uploading $source\n";
}

// close the connection
ftp_close($connection);
?>

=&GT; php脚本将托管在FTP A上的文件夹中。 感谢您的指导。

1 个答案:

答案 0 :(得分:1)

您可以使用file_put_contents

http://php.net/manual/en/function.file-put-contents.php

file_put_contents('ftp://user:pass@server/path/to/file.txt', $data);

失败时会返回false:

if(file_put_contents('ftp://user:pass@server/path/to/file.txt', $data)) {
   // ftp upload successful
} else {
   // ftp upload failed
}

为什么你当前的方法会失败?