這個例子將解釋您如何上傳FTP伺服器上的檔案。 ftp_put()命令允許上傳在伺服器上現有的檔案。對於上傳到FTP伺服器的檔案,首先你必須先登入到FTP伺服器上,搜尋源檔案上傳。定義源檔案的目標路徑。
然後檢查基本串連。如果沒有串連好,建立了串連設定使用ftp_connect($ ftp_server)。
檢查FTP伺服器串連的使用者名稱。
上傳檔案使用ftp_put()函數,你必須確定串連ID,目標檔案,源檔案和FTP_Binary。
檢查上傳的狀態,並關閉FTP串連。
結束該程式。
<?php
$ftp_server = "<IP>";
$ftp_user_name = "<username>";
$ftp_user_pass = "<password>";
$destination_file = "C:\wamp\www\projects\public_html\upload_file\".$_FILES['image']['name'];
$sourcefile = $_FILES['image']['name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // line 30
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>