简体   繁体   English

当文件路径和文件名中包含“空格”时,使用ftp_get()

[英]Using ftp_get() when there are “spaces” in the file path and filename

I need to download a file via PHP ftp_get() , but the foolish provider is using directories and file names contaning whitespace.. The file path I'm dealing with is similar to /product info/more stuff/inventory and stuff.csv 我需要通过PHP ftp_get()下载文件,但是愚蠢的提供程序正在使用包含空格的目录和文件名。.我正在处理的文件路径类似于/product info/more stuff/inventory and stuff.csv

The spaces in the path and in the filename itself is making it difficult to retrieve anything. 路径和文件名本身中的空格使检索任何内容变得困难。 I already tried the following without success: 我已经尝试了以下方法,但没有成功:

  1. $path = "/product\\ info/more\\ stuff/inventory\\ and\\ stuff.csv";

  2. $path = "/product%20info/more%20stuff/inventory%20and%20stuff.csv";

  3. $path = '"/product info/more stuff/inventory and stuff.csv"';

Thanks again for taking the time to help me out. 再次感谢您抽出宝贵时间来帮助我。

FTP?

Your third attempt, quoting the complete path , was already the recommended approach. 您的第三次尝试( 引用完整路径 )已经是推荐的方法。 Though it very much depends on the actual server implementation. 虽然这在很大程度上取决于实际的服务器实现。

FTP per RFC859 is comprised of a terminal session and a data transfer channel. 根据RFC859的FTP由一个终端会话和一个数据传输通道组成。 Basically the FTP server provides a mini-shell on the command port. 基本上,FTP服务器在命令端口上提供了一个微型外壳。 As such, typical shell string escaping rules do apply. 因此,确实适用典型的shell字符串转义规则。 URL encoding can be ruled out here. 可以在此处排除URL编码。

I'd advise first to use single quotes however. 我建议先使用单引号。 Preferrably use escapeshellarg() to apply them. 最好使用escapeshellarg()来应用它们。 And try ftp_nb_get() while at it. 并尝试ftp_nb_get()

$path = "/foo foo/bar bar/baz baz.csv";
ftp_nb_get($con, "save.csv", escapeshellarg($path), 2);

If that doesn't work, further debugging is necessary. 如果这样不起作用,则需要进一步调试。 While all ftp_* function arguments are left unprocessed , you could as well try to send a ftp_raw request. 当所有ftp_ *函数参数都未处理时 ,您也可以尝试发送ftp_raw请求。 This won't actually activate the data channel reading, but might return a more concrete error response. 这实际上不会激活数据通道的读取,但可能会返回更具体的错误响应。

print_r(ftp_raw($con, "RETR '/path to/some file.csv'\r\n"));

And I'm just gonna say it, if you're still getting a file not found error then; 我要说的是,如果您仍然遇到找不到文件的错误,那么; it's entirely possible that the file really doesn't exist at the presumed location. 很有可能该文件确实不在假定的位置。 In that case manually traverse the directory structure with ftp_nlist and ftp_rawlist with var_dump (in case of extra trailing spaces for subdirs). 在这种情况下,请使用ftp_nlistftp_rawlist以及var_dump手动遍历目录结构(如果子目录有多余的尾随空格)。

Alternatively just use PHPs ftp:// stream wrapper (which also supports PASV mode). 或者,仅使用PHP ftp://流包装器 (也支持PASV模式)。 Whose implementation is distinct from that of the ext/ftp functions. 谁的实现与ext / ftp函数的实现不同。 Here funnily enough, URL encoding is again the correct approach, but quoting still necessary ( ftp_fopen_wrapper.c does not quote itself): 有趣的是,URL编码仍然是正确的方法,但是仍然需要使用引号( ftp_fopen_wrapper.c不会引号本身):

= file_get_contents("ftp://user:pw@example.org/'path%20to/file%20and.csv'");
// Inline quotes may likely trip up some FTP server implementations..

A much better alternative though is just using cURL . 一个更好的替代方法是使用cURL

// You'll have to use the long-winded PHP curl functions of course.
print curl("ftp://.../file with spaces.csv")->exec();

Last option is just resorting to calling a Unixland client. 最后一个选择就是求助于Unixland客户程序。 (If not wget, than a plain ftp client.) (如果不是wget,则不是普通的ftp客户端。)

$url = escapeshellarg("ftp://user:pw@ftp.example.org/$path");
$file = `wget $url`; 

If you still can't retrieve any files, you'll have to look for an alternative FTP client in PHP for further debugging. 如果仍然无法检索任何文件,则必须在PHP中寻找其他FTP客户端以进行进一步的调试。 Guess who wrote one . 猜猜谁写了一个

To get a list of files or folders with spaces in the path. 获取路径中带有空格的文件或文件夹的列表。

ftp_chdir($conn, $path);

$children = ftp_rawlist($conn,'-a .');

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM