繁体   English   中英

如何使用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

我通过输入这些凭据并将端口号设置为21,尝试使用FileZilla FTP客户端连接到此服务器。 现在,我必须使用PHP代码执行相同的操作。

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

一种可能的解决方案是将文件本地保存后,将其上传到远程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/zh/ref.ftp.php

希望有帮助:-)


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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