Make FTP connection in Laravel not working with Flysystem library.

huangapple go评论93阅读模式
英文:

Make FTP connection in Laravel not working with Flysystem library

问题

你好,我理解你遇到了FTP连接问题。你提供的代码似乎有一些问题,但是你也怀疑可能是服务器配置的问题。以下是你的代码的问题部分:

  1. 在第一个代码块中,你定义了$adapter,但是没有设置$ftp_server$ftp_username$ftp_password等必要的FTP连接信息。请确保这些变量被正确设置。

  2. 在第二个代码块中,你使用了ftp_connect来连接FTP服务器,但是在尝试上传文件之前,你并没有设置被动模式。你可以在连接后使用ftp_pasv($connId, true);来启用被动模式,这对于某些FTP服务器是必需的。

  3. 如果你在使用被动模式时遇到Warning: ftp_fput(): php_connect_nonb() failed: No error (0)错误,这可能是因为服务器配置问题。你可以尝试联系服务器管理员或者检查服务器的防火墙和FTP服务器配置以确保被动模式正常工作。

  4. 如果使用非被动模式时出现Warning: ftp_fput(): Illegal PORT command错误,这可能是服务器配置问题。尝试与服务器管理员协商正确的配置。

  5. 请确保你的FTP服务器地址、端口、用户名和密码等信息都是正确的,与FileZilla中的设置一致。

总之,问题可能出现在代码和服务器配置之间的兼容性问题。你可以与服务器管理员联系,以确保服务器配置正确,并且尝试根据服务器要求调整你的代码。

英文:

Hello i am trying to make a FTP connection, using Laravel 8 and Flysystem library, however all my tries have failed. My ftp details work on Fillezilla

Here is my code

  1. use League\Flysystem\Filesystem;
  2. use League\Flysystem\Adapter\Ftp;
  1. $adapter = new Ftp([
  2. 'host' => $ftp_server,
  3. 'username' => $ftp_username,
  4. 'password' => $ftp_password,
  5. 'port' => 2526,
  6. 'passive' => false,
  7. 'ssl' => false,
  8. 'tls' =>false,
  9. 'timeout' => 400,
  10. 'utf8' => false,
  11. 'passive' => true,
  12. 'transferMode' => FTP_BINARY,
  13. 'systemType' => null, // 'windows' or 'unix'
  14. 'ignorePassiveAddress' => null, // true or false
  15. 'timestampsOnUnixListingsEnabled' => false, // true or false
  16. 'recurseManually' => true // true
  17. ]);
  18. $filesystem = new Filesystem($adapter);
  19. // Check if the connection is successful
  20. if ($filesystem->has($path)) {
  21. echo "FTP connection successful!";
  22. } else {
  23. echo "FTP connection failed!";
  24. }

It is giving me always FTP connection failed! and i can't find any solution

Thanks in advance for your help

edit:

I decided to try to do a simple file upload in a simple plain php file to check if it works, and i am having the same problem

for this i used this code

  1. // File details
  2. $fileName = 'example.txt';
  3. $fileContents = 'This is the content of the file.';
  4. // Create a temporary file
  5. $tempFile = tmpfile();
  6. fwrite($tempFile, $fileContents);
  7. fseek($tempFile, 0); // Move the file pointer to the beginning of the file
  8. // Connect to FTP server
  9. $connId = ftp_connect($ftpServer, $ftpPort);
  10. if ($connId) {
  11. // Login to FTP server
  12. $loginResult = ftp_login($connId, $ftpUsername, $ftpPassword);
  13. if ($loginResult) {
  14. // Perform FTP operations here (e.g., put, get)
  15. // echo "conexão";
  16. // Logout and close the connection
  17. // ftp_pasv($connId, true);
  18. // $localFile = 'C:\xampp\htdocs\FtpTest\file\file.txt';
  19. // $remoteFile = '/file.txt';
  20. // //ftp_mkdir($connId, $remoteFile);
  21. // // Upload file to FTP server
  22. // if (ftp_put($connId, $remoteFile, $localFile, FTP_BINARY)) {
  23. // echo 'File uploaded successfully.';
  24. // } else {
  25. // echo 'File upload failed.';
  26. // }
  27. // ftp_close($connId);
  28. // Upload the temporary file to the FTP server
  29. // ftp_pasv($connId, true);
  30. $upload = ftp_fput($connId , $fileName, $tempFile, FTP_ASCII);
  31. if (!$upload) {
  32. die('Failed to upload the file');
  33. }
  34. // Close the temporary file
  35. fclose($tempFile);
  36. // Close FTP connection
  37. ftp_close($ftpConnection);
  38. echo 'File created successfully.';
  39. } else {
  40. echo 'FTP login failed.';
  41. }
  42. } else {
  43. echo 'Could not connect to FTP server.';
  44. }

If i set my passive as true i get this error

  1. Warning: ftp_fput(): php_connect_nonb() failed: No error (0)

if i set it false i get this error

  1. Warning: ftp_fput(): Illegal PORT command

So i am starting to believe that the problem is not exactly to my code, but maybe something with my server, however i find it strange, since i can enter it , by using filezilla

答案1

得分: 0

这是要翻译的内容:

"目前还没有完全解决,但找出了问题所在。问题出现在我试图发送文件的服务器上。尽管在FileZilla中建立了连接,但刚刚发现它不允许我在那里传输文件(因为已经建立了连接,我之前假设一切在那里都正常工作)。

因此,我相信我在这里放置的代码以及@Hercules73建议的代码都正常工作。谢谢你。"

英文:

It is not exactly solved yet, but found out where my problem lies. It is on my server where i am trying to send my files to. Despite a connection was made in filezilla, just found out that it isn't allowing me to transfer files inside there (Since the connection was made, i assumed before, that everything was working fine in there).

So i believe the code i put in here, and the one that @Hercules73 suggested works fine. Thank you

huangapple
  • 本文由 发表于 2023年5月10日 20:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定