2015-08-19 222 views
1

我想要一個文件MYFILE.csv到遠程FTP。以下是腳本。連接部分工作,但不是文件上傳。我收到「上傳時出現問題」消息。PHP腳本將文件上傳到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); 
?> 

回答

0

我看到dest文件夾是「/ 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); 
    ?>