【问题标题】:Why I'm not able to upload image file to the remote FTP server even after successfully establishing a connection to it in PHP?为什么即使在 PHP 中成功建立与远程 FTP 服务器的连接后,我也无法将图像文件上传到远程 FTP 服务器?
【发布时间】:2014-12-02 19:10:43
【问题描述】:

我想连接到一个远程 FTP 服务器并将图像文件上传到该服务器的某个特定位置。我能够连接到服务器,但无法上传文件。

每次函数 ftp_put() 都返回 false。我调试了很多代码,但我不明白我在哪里犯了错误。

以下是我的代码:

HTML 代码:

<form method="post" enctype="multipart/form-data" action="xyz.php">
  <input type="file" name="student_image" id="student_image" />                  
</form>

PHP代码(xyz.php):

  $t = time();
  $allowed_image_extension = array("jpg","jpeg","gif","png","JPG","JPEG","GIF","PNG");
  if(!empty($_FILES['student_image']['name'])) {
    $ext = pathinfo($_FILES['student_image']['name'], PATHINFO_EXTENSION);     
    $student_image_name = 'student_'.$t.'.'.$ext;
    $_POST['student_name'] = $student_image_name;

    $ftp_server="52.237.5.85"; 
    $ftp_user_name="myservercreds"; 
    $ftp_user_pass="MyServerCreds";

    $file = $_FILES['student_image']['name'];//tobe uploaded 
    $remote_file = "/Students/".$_POST['student_name']; 

    // set up basic connection 
    $conn_id = ftp_connect($ftp_server);  

    // login with username and password 
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 


    // upload a file 
    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) { 
      echo "successfully uploaded $file\n"; 
      exit; 
    } else { 
      echo "There was a problem while uploading $file\n"; 
      exit; 
    } 
    // close the connection 
    ftp_close($conn_id);
  }

在调试过程中,除了函数ftp_put() 中的一处之外,我得到了 True 和资源。

【问题讨论】:

  • 可以ftp_chdir()Students的目录吗?
  • /Students/foo.txt?你的上传真的是在文件系统的根目录下吗?除非那是 chroot'ed ftp,否则您实际上是在尝试使用 c:\students\foo.txt
  • 你需要在 /var/log/{your-ftp-log} 上拖尾 - 这可能是权限问题

标签: php file-upload ftp image-uploading remote-server


【解决方案1】:

我相信这可能是因为您将绝对路径作为 $remote_file 传递。 ftp_put 失败应该意味着无论出于何种原因,您都无法写入该位置。我相信更改它以便在路径之前没有 / 应该可以解决问题(请注意,该文件将相对于 ftp 用户的主目录,这通常是您通过 ftp 连接时首先显示的目录) .

这应该是这样的:

$file = $_FILES['student_image']['tmp_name']; 
$remote_file = "Students/".$_POST['student_name']; 

编辑:编辑答案以包含 tmp_name 而不是名称,这是一个两部分。

【讨论】:

    【解决方案2】:

    您使用了错误的文件名:

    $file = $_FILES['student_image']['name'];//tobe uploaded 
                                    ^^^^^^^^
    

    这是用户计算机上文件的文件名。 PHP 不使用它。它将上传文件放入一个随机文件名中,并将该名称存储在['tmp_name'] 中。你想要的

    $file = $_FILES['student_image']['tmp_name'];
    

    相反。由于您使用了错误的文件名,即服务器上不存在的文件,ftp_put 正确返回 false,因为无法读取该不存在的文件。

    【讨论】:

      【解决方案3】:

      你为什么不尝试获取 ftp 错误:

      $trackErrors = ini_get('track_errors');
      ini_set('track_errors', 1);
      if (!@ftp_put($my_ftp_conn_id, $tmpRemoteFileName, $localFileName, FTP_BINARY)) {
         // error message is now in $php_errormsg
         $msg = $php_errormsg;
         ini_set('track_errors', $trackErrors);
         throw new Exception($msg);
      }
      

      @ref:How to get the FTP error when using PHP

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 2014-09-27
        相关资源
        最近更新 更多