2013-05-06 66 views
0

我試圖使用ftp將產品上傳到我的服務器。但是我提示以下錯誤消息: FTP連接失敗試圖連接到.....使用ftp_connect將文件上傳到ftp失敗

下面是PHP腳本:

if (isset($submit)){ 

//connect to ftp server 
$ftp_server="123456shop.bugs3.com"; 
//ftp user name 
$ftp_user_name="u12345"; 
//ftp username password 
$ftp_user_pass="abcde"; 

$con_id=ftp_connect($ftp_server); 
// login with username and password 
$login_result = ftp_login($con_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"."....."; 
} 



$allowedExts = array("jpg", "jpeg", "gif", "png"); 
$extension = end(explode(".", $_FILES["file"]["name"])); 
if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/png") 
|| ($_FILES["file"]["type"] == "image/jpg")) 
&& ($_FILES["file"]["size"] < 2000000) 
&& in_array($extension, $allowedExts)) 
{ 
if ($_FILES["file"]["error"] > 0) 
{ 
echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
} 
else 
{ 
echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 

if (file_exists("../product_images/" . $_FILES["file"]["name"])) 
    { 
    echo $_FILES["file"]["name"] . " already exists. "; 
    } 
else 
    { 
    $filep=$_FILES['file']['tmp_name']; 
    // upload the file 
$upload = ftp_put($conn_id, '/home/u408392962/public_html/product_images/', $filep,  FTP_BINARY); 

// check upload status 
if (!$upload) { 
    echo "FTP upload has failed!"; 
} else { 
    echo "Uploaded $name to $ftp_server "; 
} 
    /*move_uploaded_file($_FILES["file"]["tmp_name"], 
    "../product_images/" . $_FILES["file"]["name"]); 
    echo "Stored in: " . "../product_images/" . $_FILES["file"]["name"];*/ 

    $sql = "INSERT INTO product (name,price,description,type,qty,IsSpecial,categoryID,IsNew) VALUES ('$_POST[name]', '$_POST[price]', '$_POST[description]','$_POST[type]','$_POST[qty]','$_POST[IsSpecial]','$_POST[categoryID]','$_POST[IsNew]')"; 
    $recordset2 = mysql_query($sql) or die(mysql_error()); 
    echo "<script language=javascript>alert('SUCCESSFULLY Add!')</script>"; 
    echo "<SCRIPT language='Javascript'> 
      document.location=\"\list.php\"; 
     </SCRIPT>"; 
    } 
} 
} 
else 
{ 
echo "Invalid file"; 
} 

} 

誰能幫助我? 非常感謝!

+0

腳本看起來不錯...檢查您的憑據一次。確保它們是正確的。 – 2013-05-06 10:09:51

+0

$ con_id = ftp_connect($ ftp_server)或die(「無法連接」);檢查與服務器建立的連接。嘗試使用error_reporting(1) – Shin 2013-05-06 10:11:19

+0

嘗試連接到公共ftp服務器,查看您的連接是否正常工作。如果成功,則表示問題來自您的ftp服務器。 – Alarid 2013-05-06 10:13:04

回答

0

嘗試啓用被動模式連接,某些服務器在未被動模式下無法接受。

嘗試使用ftp_pasv()

+0

謝謝,但這並不奏效,到目前爲止的問題是,我現在可以連接到ftp文件,但是我無法將文件上傳到服務器,似乎我給出的路徑不正確 – user2295136 2013-05-06 13:11:56

+0

@ user2295136,你是否能夠使用ftp_nlist列出文件? – Shin 2013-05-06 13:21:25

+0

謝謝你,這是由於實際上被路由到的路徑 – user2295136 2013-05-07 13:58:06