英文:
Make FTP connection in Laravel not working with Flysystem library
问题
你好,我理解你遇到了FTP连接问题。你提供的代码似乎有一些问题,但是你也怀疑可能是服务器配置的问题。以下是你的代码的问题部分:
-
在第一个代码块中,你定义了
$adapter
,但是没有设置$ftp_server
、$ftp_username
、$ftp_password
等必要的FTP连接信息。请确保这些变量被正确设置。 -
在第二个代码块中,你使用了
ftp_connect
来连接FTP服务器,但是在尝试上传文件之前,你并没有设置被动模式。你可以在连接后使用ftp_pasv($connId, true);
来启用被动模式,这对于某些FTP服务器是必需的。 -
如果你在使用被动模式时遇到
Warning: ftp_fput(): php_connect_nonb() failed: No error (0)
错误,这可能是因为服务器配置问题。你可以尝试联系服务器管理员或者检查服务器的防火墙和FTP服务器配置以确保被动模式正常工作。 -
如果使用非被动模式时出现
Warning: ftp_fput(): Illegal PORT command
错误,这可能是服务器配置问题。尝试与服务器管理员协商正确的配置。 -
请确保你的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
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp;
$adapter = new Ftp([
'host' => $ftp_server,
'username' => $ftp_username,
'password' => $ftp_password,
'port' => 2526,
'passive' => false,
'ssl' => false,
'tls' =>false,
'timeout' => 400,
'utf8' => false,
'passive' => true,
'transferMode' => FTP_BINARY,
'systemType' => null, // 'windows' or 'unix'
'ignorePassiveAddress' => null, // true or false
'timestampsOnUnixListingsEnabled' => false, // true or false
'recurseManually' => true // true
]);
$filesystem = new Filesystem($adapter);
// Check if the connection is successful
if ($filesystem->has($path)) {
echo "FTP connection successful!";
} else {
echo "FTP connection failed!";
}
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
// File details
$fileName = 'example.txt';
$fileContents = 'This is the content of the file.';
// Create a temporary file
$tempFile = tmpfile();
fwrite($tempFile, $fileContents);
fseek($tempFile, 0); // Move the file pointer to the beginning of the file
// Connect to FTP server
$connId = ftp_connect($ftpServer, $ftpPort);
if ($connId) {
// Login to FTP server
$loginResult = ftp_login($connId, $ftpUsername, $ftpPassword);
if ($loginResult) {
// Perform FTP operations here (e.g., put, get)
// echo "conexão";
// Logout and close the connection
// ftp_pasv($connId, true);
// $localFile = 'C:\xampp\htdocs\FtpTest\file\file.txt';
// $remoteFile = '/file.txt';
// //ftp_mkdir($connId, $remoteFile);
// // Upload file to FTP server
// if (ftp_put($connId, $remoteFile, $localFile, FTP_BINARY)) {
// echo 'File uploaded successfully.';
// } else {
// echo 'File upload failed.';
// }
// ftp_close($connId);
// Upload the temporary file to the FTP server
// ftp_pasv($connId, true);
$upload = ftp_fput($connId , $fileName, $tempFile, FTP_ASCII);
if (!$upload) {
die('Failed to upload the file');
}
// Close the temporary file
fclose($tempFile);
// Close FTP connection
ftp_close($ftpConnection);
echo 'File created successfully.';
} else {
echo 'FTP login failed.';
}
} else {
echo 'Could not connect to FTP server.';
}
If i set my passive as true i get this error
Warning: ftp_fput(): php_connect_nonb() failed: No error (0)
if i set it false i get this error
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论