简体   繁体   中英

Uploading file to ftp using ftp_connect fails

I am trying to upload product to my server using ftp. However I am given the following error message: FTP connection has failed!Attempted to connect to .....

Below is php script:

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";
}

}

Can anyone help me with this? Thanks a lot!!!

Try to connect with passive mode enabled,Some server could not accept without passive mode.

Try using ftp_pasv()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM