php 在伺服器上傳檔案

來源:互聯網
上載者:User

這個例子將解釋您如何上傳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);

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.