使用ftp_connect将文件上传到ftp失败

时间:2013-05-06 10:04:51

标签: php

我正在尝试使用ftp将产品上传到我的服务器。但是,我收到以下错误消息: 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";
}

}

任何人都可以帮我吗? 非常感谢!!!

1 个答案:

答案 0 :(得分:0)

尝试连接已启用被动模式,某些服务器无法接受被动模式。

尝试使用ftp_pasv()