2014-10-09 95 views
0

我已經嘗試文件上傳到服務器使用FTP連接在PHP和它不工作,其連接,但得到錯誤,如「連接到XXXXXXXXXXX,用戶XXXXXXXXXXXXX FTP上傳失敗!」 我曾嘗試下面的代碼,請通過糾正它幫助..圖像不存儲在FTP

<!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 = "XXXX"; 
$ftp_user_name = "XXXXX"; 
$ftp_user_pass = "XXXXXXXXXX"; 
$destination_file = "./imagetest/123/"; 
$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); 
?> 
+0

的'$ destination_file'變量似乎只能是一個目錄,而不是一個文件名。根據[PHP手冊](http://php.net/manual/en/function.ftp-put.php),'ftp_put()'中的第二個參數應該是一個文件路徑。 – Crackertastic 2014-10-09 11:20:47

+0

'$ destination_file =「./imagetest/123 /」;'它的目錄只有 – Redsun 2014-10-09 11:33:57

+0

我看到它只是一個目錄。我說的是它需要是一個文件名。按照我之前評論中的鏈接閱讀手冊。它將全部列出該函數期望的參數 – Crackertastic 2014-10-09 11:43:06

回答

3

您必須在路徑後添加文件名。

替換下面的代碼,並嘗試,

$destination_file = "./imagetest/123/".$_FILES['file']['name']; 
0

$ destination_file不是一個文件時,它是一個目錄。

此外,您還需要更改目錄以上傳文件。

例如,

<?php 
    ftp_chdir($conn, $destination_directory); 
    ftp_put($conn, $filename , $source_file, FTP_BINARY); 
?> 
+0

僅用於其目錄,... – Redsun 2014-10-09 11:31:14

+0

您需要指定目錄和文件名。不僅是目錄。 – CnR 2014-10-09 11:40:16