Good afternoon, I get base64 encoded images on my server, how can I upload them to FTP cloud via ftp_put?

Now I try to do it like this, but it does not work.

$imsrc = base64_decode($_POST['base64']); $connect = ftp_connect($host); if(!$connect) die("error connect"); $result = ftp_login($connect, $login, $pass); if ($result==false) die("error res"); if (ftp_chdir($connect, $path)) { if (ftp_put($connect, $aname, $imsrc, FTP_BINARY)) echo $aname; else echo "error"; } 

Throws error.

I tried to add more

 $tmp = fopen('php://memory', 'r+'); fputs($tmp, $imsrc); rewind($tmp); ... if (ftp_put($connect, $aname, $tmp, FTP_BINARY)) ... 

And again error.

Any ideas?

    1 answer 1

    ftp_put accepts paths only. Not the contents of the file, or file stream. Save the contents of the file and fill normally, then you can delete.

     $imsrc = base64_decode($_POST['base64']); file_put_contents('temporary/'.$aname,$imsrc); $connect = ftp_connect($host); if(!$connect) die("error connect"); $result = ftp_login($connect, $login, $pass); if ($result==false) die("error res"); if (ftp_chdir($connect, $path)) { if (ftp_put($connect, $aname, 'temporary/'.$aname, FTP_BINARY)) echo $aname; else echo "error"; } unlink('temporary/'.$aname); 
    • Thanks, it helped. - AndroidProf