2014-10-13 117 views
0

我已經嘗試文件上傳到服務器使用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); 
?> 
+0

$ destination_file正確指定您的目錄...並確保您的根目錄中有該目錄... 根據您的代碼,目錄是動態的,因爲您已將文件名稱附加到目錄路徑中... –

+0

警告:ftp_put():無法創建文件。 in /homeb/home/XXXXXXXXXX/www/upload.php @Fred – Redsun

+0

remove。$ _ FILES ['file'] ['name'];在$ destination_file =「imagetest/123 /".$_ FILES ['file'] ['name'];並嘗試...我認爲問題是與目錄... –

回答

1

我測試你的代碼,有一點硬的有時間讓它工作,但是對我來說有效的是使用/http_docs/public_html作爲基本/根文件夾。

  • 根文件夾名稱因託管服務而異,因此請相應修改。

I.e.並與一些修改:

<?php 
$ftp_server = "XXXXXX"; 
$ftp_user_name = "XXXXXXX"; 
$ftp_user_pass = "XXXXXXXX"; 

$folder = "/http_docs/imagetest/123/"; 

$destination_file = $folder . $_FILES['file']['name']; 

$source_file = $_FILES['file']['tmp_name']; 

// rest of code 

旁註:

使用全路徑名。

即使:/var/user/you/public_html/它不起作用。

+0

thanx我的是「www」,工作正常,... – Redsun

+0

@Redsun不客氣,很高興它解決了,*歡呼聲* –