用PHP连接到FTP,密码为@ [英] Connect to FTP with PHP with a password that has @
问题描述
我遇到以下问题:
我需要连接到FTP并读取一个CSV文件。它的主要问题是密码有@,$,%...我如何连接especials字符?我尝试了以下方式进行连接:
文件打开
$ filename ='ftp:// user:p @ s($word@ftp.myftp.url/file.csv';
$ handle = fopen($ filename,r)或die(Error) ;
FTP登录
$ ftp_server =ftp.myftp.url / file.csv;
$ ftp_user =user;
$ ftp_pass =p @ s($ word;
$ conn_id = ftp_connect($ ftp_server)或死(无法连接到$ ftp_server);
$ login_result = ftp_login($ ftp_server,$ ftp_user,$ ftp_pass)或死(无法连接到2 );
$ data = file_get_contents('ftp.myftp.url / file.csv');
感谢所有!
您的两段代码有两个不同的问题。 b
$ b
文件打开
$ filename ='ftp:// user:p @ s($word@ftp.myftp.url/file.csv';
$ handle = fopen($ filename,r)或死(错误);
问题在于 @
,正如您已经正确猜测的那样,因为它在 URL语法中有含义(作为证书和主机名之间的分隔符)。
您必须 URL编码 @
到%40
:
$ filename ='ftp:// user:p%40s($word@ftp.myftp.url/file.csv' ;
您提到实际密码也有%
。那必须以URL编码为 %25
。
FTP登录
$ ftp_server =ftp.myftp.url / file.csv;
$ ftp_user =user;
$ ftp_pass =p @ s($ word;
$ conn_id = ftp_connect($ ftp_server)或die(无法连接到$ ftp_server);
$ login_result = ftp_login( $ ftp_server,$ ftp_user,$ ftp_pass)或die(无法连接到2);
在这里,问题不是 @
,因为不包含任何URL(%
会是一个问题)在这里,问题是 $
,因为你是使用双引号,所以 $ word
被替换为一个值(可能是未定义的)变量 word
,有效地使密码为 p @ s(
only。
使用单引号,以避免将 $
解释为变量:
$ ftp_pass ='p @ s($ word';
也可以用<$ c转义 $
$ b $ $ p $ code $ $ ftp_pass =p @ s(\ $ word ;
I have the following problem:
I need connect to FTP and read one CSV file. The main problem it's password has @, $, %... How can I connect with especials characters? I tried the following ways to connect:
FILE OPEN
$filename = 'ftp://user:p@s($word@ftp.myftp.url/file.csv';
$handle = fopen($filename, "r") or die("Error");
FTP LOGIN
$ftp_server = "ftp.myftp.url/file.csv";
$ftp_user = "user";
$ftp_pass = "p@s($word";
$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login_result = ftp_login($ftp_server, $ftp_user, $ftp_pass) or die("Could not connect to 2");
$data = file_get_contents('ftp.myftp.url/file.csv');
Thanks for all!
You have two distinct problems with your two pieces of code.
FILE OPEN
$filename = 'ftp://user:p@s($word@ftp.myftp.url/file.csv'; $handle = fopen($filename, "r") or die("Error");
Here, the problem is the @
, as you have correctly guessed, as it has a meaning in the URL syntax (as a separator between credentials and hostname).
You have to URL-encode the @
to %40
:
$filename = 'ftp://user:p%40s($word@ftp.myftp.url/file.csv';
You mentioned that the actual password also has %
. That has to be URL-encoded to %25
.
FTP LOGIN
$ftp_server = "ftp.myftp.url/file.csv"; $ftp_user = "user"; $ftp_pass = "p@s($word"; $conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login_result = ftp_login($ftp_server, $ftp_user, $ftp_pass) or die("Could not connect to 2");
Here, the problem is not @
, as no URL is involved (neither %
would be a problem). Here, the problem is the $
, as you are using double-quotes, so $word
is replaced with a value of (probably undefined) variable word
, effectively making the password be p@s(
only.
Use single quotes to avoid the $
being interpreted as a variable:
$ftp_pass = 'p@s($word';
Or escape the $
with \
:
$ftp_pass = "p@s(\$word";
这篇关于用PHP连接到FTP,密码为@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!