PHP脚本将文件上传到FTP

问题描述 投票:1回答:2

我想将文件MYFILE.csv到远程FTP。 下面是脚本。 连接部分有效,但文件上传无效。 我收到“上传时出现问题”消息。

谢谢您的帮助。

<?php
$server = 'ftp.website.com' ;//Address of ftp server
$user_name = 'MYUSERNAME'; // Username
$password = 'MYPASSWORD'; // Password

$source_file = '/home/MYFILES.csv'; 
$dest = '/in/';

// set up basic connection
$connection = ftp_connect($server, 21) or die("Couldn't connect to     $ftp_server"); 

echo "can connect";
echo "<br />";
// login with username and password
ftp_login($connection, $user_name, $password) or die("Cannot login");
echo "can login";
echo "<br />";

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

// close the connection
ftp_close($connection);
?>
php file-upload ftp
2个回答
0
投票

我看到目标文件夹是“ / in /”。

您确定它不尝试将其放在ftp的根文件夹中吗? (这可能属于root用户,这就是它失败的原因)


0
投票

找到了解决方案:

?php
$server = 'ftp.WEBSITE.com' ;//Address of ftp server
$user_name = 'MYUSERNAME'; // Username
$password = 'MYPASSWORD'; // Password

$source_file = '/home/MYFILE.csv'; 
$dest = '/in/MYFILE.csv';

// set up basic connection 
$connection = ftp_connect($server, 21) or die("Couldn't connect to     $ftp_server"); 
echo "can connect";
 echo "<br />";

// login with username and password
ftp_login($connection, $user_name, $password) or die("Cannot login");
echo "can login";
echo "<br />";
// upload a file
ftp_put($connection, $dest, $source_file, FTP_ASCII) or die ("Cannot upload");

// close the connection
    ftp_close($connection);
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.