我已經嘗試文件上傳到服務器使用FTP連接在PHP和它不工作,其連接,但得到錯誤如「連接到XXXXXXXXXXX,用戶XXXXXXXXXXXXX FTP上傳失敗!我曾嘗試下面的代碼,請通過糾正它幫助..使用PHP將圖像上傳到FTP
image.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php的
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$destination_file = "imagetest/123/".$_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// 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);
// 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);
?>
$ destination_file正確指定您的目錄...並確保您的根目錄中有該目錄... 根據您的代碼,目錄是動態的,因爲您已將文件名稱附加到目錄路徑中... –
警告:ftp_put():無法創建文件。 in /homeb/home/XXXXXXXXXX/www/upload.php @Fred – Redsun
remove。$ _ FILES ['file'] ['name'];在$ destination_file =「imagetest/123 /".$_ FILES ['file'] ['name'];並嘗試...我認爲問題是與目錄... –