简体   繁体   English

为什么即使在PHP中成功建立了与镜像文件的连接后,也无法将其上传到远程FTP服务器?

[英]Why I'm not able to upload image file to the remote FTP server even after successfully establishing a connection to it in PHP?

I want to connect to one remote FTP server and upload image file to that server at some specific location. 我想连接到一台远程FTP服务器并将图像文件上传到该服务器的某个特定位置。 I'm able to connect to the server but not able to upload the file. 我可以连接到服务器,但无法上传文件。

Every time the function ftp_put() is returning false. 每次函数ftp_put()返回false时。 I debugged the code a lot but I'm not understanding where I'm making the mistake. 我调试了很多代码,但是我不明白我在哪里犯错。

Following is my code: 以下是我的代码:

HTML code: HTML代码:

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

PHP code(xyz.php): 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);
  }

During debugging I got True and resource everywhere except at one place in a function ftp_put() . 在调试过程中,除了函数ftp_put()某个地方以外,我到处都有True和资源。

I believe this is likely due to you passing an absolute path as the $remote_file. 我相信这很可能是由于您传递了绝对路径作为$ remote_file。 A failure on ftp_put should mean you can not write to that location, for whatever reason. ftp_put上的故障应该意味着您无论出于何种原因都无法写入该位置。 I believe changing this so that it does not have the / before the path should fix the issue (note that the file will be relative to the ftp user's home dir, which is typically the directory that you are first displayed when you connect via ftp). 我相信要对此进行更改,以便在路径应解决该问题之前不带/(请注意,该文件将相对于ftp用户的主目录,该目录通常是通过ftp连接时首次显示的目录) 。

This should look like this: 看起来应该像这样:

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

EDIT: Edited answer to include tmp_name instead of name, this is a two-parter. 编辑:编辑的答案包括tmp_name而不是名称,这是一个两部分。

You're using the wrong filename: 您使用了错误的文件名:

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

That is the filename of the file on the USER's computer. 那是用户计算机上文件的文件名。 PHP doesn't use that. PHP不使用它。 It puts the upload into a random file name, and stores that name in ['tmp_name'] . 它将上传文件放入一个随机文件名,并将该文件名存储在['tmp_name'] You want 你要

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

instead. 代替。 Since you're using the wrong filename, a file which DOESN'T exist on the server, ftp_put is properly returning false since that non-existent file can't be read. 由于使用的文件名错误,服务器上不存在该文件,因此ftp_put正确返回false,因为无法读取该不存在的文件。

also why don't you try to get the ftp errors: 也为什么不尝试获取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 @ref: 使用PHP时如何获取FTP错误

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

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