Search code examples
phpcentos8

PHP ftp_put() expects parameter 1 to be resource, boolean given when trying to upload a file to a remote ftp server


I'm trying to upload a submitted CV from job applicants to a remote ftp server.

I've wrapped all the steps into "if...else echo" statements and they are all successful (fopen, ftp_connect, ftp_login), but it gets stuck at the fpt_fput statement with this error:

ftp_put() expects parameter 1 to be resource, boolean given in /var/www/*******/phpmailer/sendmail.php on line 286

I've checked whether it could be SELinux, but it's not running on that server.

Here is the code for that section in the php file:

$location = "uploads/" . $finalCV;
//move_uploaded_file($_FILES['applCV']['tmp_name'], $location);

//Move uploaded & renamed CV to server
if($fp = fopen($location, 'r'))
  echo "File Open Successful. ";
  else {
    echo "File Open Unsuccessful. ";
  }

if($conn_id = ftp_connect("some.server"))
  echo "FTP Connection Established Successfully. ";
else {
  echo "FTP Connection Failed. ";
  }

$ftpuser = "wynand";
$ftppasswd = "********";

if($login = ftp_login($conn_id, $ftpuser, $ftppasswd)) {
  echo "FTP Login Successful. ";
}
 else {
   echo "FTP Login Unsuccessful. ";
 }

if(ftp_put($login, $location, $fp, FTP_ASCII)) {
  echo "Successfully uploaded CV. ";
}
  else {
    echo "There was a problem uploading CV. ";
  }

Solution

  • Your $login have result ftp_login(), but ftp_put() want resource $ftp

    Exemple from doc ftp_put

    <?php
    $file = 'somefile.txt';
    $remote_file = 'readme.txt';
    
    // 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";
    } else {
       echo "There was a problem while uploading $file\n";
    }
    
    // close the connection
    ftp_close($conn_id);
    ?>
    

    First param of ftp_put() take ftp_connect() result. Try

    if(ftp_put($conn_id, $location, $fp, FTP_ASCII)) {
      echo "Successfully uploaded CV. ";
    }
    

    If you want use open file pointer on the local file (in your code you use it:)) try ftp_fput()