如何使用PHP连接到远程FTP服务器并将图像文件上传到特定位置


How to connect to remote FTP server and upload an image file to it at specific location using PHP?

我是高级PHP的新手。我在我的网站上使用 PHP 5.5.18

场景是,我有一个远程FTP服务器。我想连接到它,在该FTP服务器上的特定位置上传图像文件,将该文件保存在FTP服务器上的该文件夹中。

我已经成功地编写了一个代码,将图像文件上传到我正在执行PHP脚本的同一服务器。这对我来说绝对没问题。

但是现在我必须连接到其他远程FTP服务器并将图像文件上传到其中。这对我来说是一件全新的事情。我对这个概念完全是空白的。

有人可以在这方面帮助我吗?

以下是我编写的工作 PHP 代码,用于将文件上传到运行 PHP 脚本的同一服务器。

<?php
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
      echo "File is an image - " . $check["mime"] . ".";
      $uploadOk = 1;
    } else {
      echo "File is not an image.";
      $uploadOk = 0;
    }
  }
  // Check if file already exists
  if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
  }
  // Check file size
  if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
  }
  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
      echo "Sorry, there was an error uploading your file.";
    }
  }
?>

以下是我的 FTP 服务器的示例代表凭据:

IP: 54.220.3.82
Username: myservercreds
Password: MyServerCreds

我尝试使用 FileZilla FTP 客户端连接到此服务器,方法是输入这些信条并将端口号设置为 21。现在我必须使用 PHP 代码做同样的事情。

我想将此上传的图像文件存储到名为"图像"的文件夹中。此文件夹应具有所有权限(读取,写入和删除(。如果该文件夹已存在,请不要重新创建它。

一种可能的解决方案是在本地保存文件后将文件上传到远程 FTP 服务器。

假设您已在本地保存文件,路径$target_file ,就像在您的代码中一样。现在你可以做这样的事情:

<?php
$remote_file = "images/" . $target_file;  // since you want to upload to a directory called "images".
$conn_id = ftp_connect("ftp.foo.bar") or die("Error in FTP connection");  // the FTP server you want to connect to.
$login_status = ftp_login($conn_id, "Username", "PaSswoRd") or die("Error in FTP login");  // Login to the FTP server.
// Now go ahead, and upload the file.
$upload_status = ftp_put($conn_id, $remote_file, $target_file, FTP_BINARY);
if (!$upload_status)
{
    // do whatever it is that you want to do when you are unable to upload the file.
}
// Close the FTP connection after you are done.
ftp_close($conn_id);
?>

当然,这是假设您的 FTP 服务器中有一个名为 images/ 的目录。如果没有,您可以使用FTP客户端创建它,或者使用PHP本身使用。

ftp_mkdir($conn_id, "images");

笔记:

  • 您可以使用 ftp_chmod() 方法设置权限。

  • 如果您不希望上传文件后将其保留在服务器上,可以使用 unlink() 删除它们。

    更好的解决方案可能是仅上传不带痛风将其保存在本地。只需在上面示例中使用$target_file的地方使用$_FILES['fileToUpload']['tmp_name']即可。并且不要使用 move_uploaded_file() 保存上传的文件,就像在您的代码中一样。

  • 您可能需要的所有内容都可能在文档中。一定要看看。文档并不像听起来那么可怕。双关语本意:)

    FTP 函数文档:http://php.net/manual/en/ref.ftp.php

希望对:-)有所帮助


编辑:感谢@halfer对这个问题的评论。