我有一個HTML瀏覽類型和提交按鈕的窗體。我使用瀏覽按鈕選擇一個文件並提交表單。在表單提交後,調用下面的代碼。如何在PHP中通過ftp上傳文件?
$conn_id="myid";
$conn_id = ftp_connect ('server');
$ftp_user_name="username";
$ftp_user_pass="password";
// 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!" ;
exit;
} else {
echo "Connected to for user $ftp_user_name" ;
}
// upload the file
$upload = ftp_put($conn_id, "images/signatures/" . $fileName , $_FILES['tmp_name'] , FTP_BINARY);
// check upload status
if(!$upload){
echo "FTP upload has failed!" ;
} else {
echo "Successfully Uploaded." ;
}
但它產生以下警告:
Warning: ftp_put(): Filename cannot be empty in /var/www/echdp/_ProviderSignature.php on line 70 FTP upload has failed!
但是,當我硬編碼在上面的代碼中的源路徑然後上傳在服務器上的文件:
$upload = ftp_put($conn_id, "images/signatures/myfile.txt" , "/var/www/images/hello.txt" , FTP_BINARY);
你確定你的html表單有'enctype ='multipart/form-data''屬性嗎?否則,$ _FILES數組將不會被填充,您將看到您所看到的錯誤。 – JCD 2010-08-17 13:15:52