本文介绍了当有“空格”时使用ftp_get()在文件路径和文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过PHP下载文件由终端会话和数据传输通道组成。基本上FTP服务器在命令端口上提供了一个迷你shell。因此,典型的外壳字符串转义规则适用。 URL编码可以排除在这里。



我建议先使用单引号。最好使用来应用它们。然后尝试。 p>

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

如果这不起作用,则需要进一步调试。虽然所有ftp_ *函数参数请求。这不会实际激活数据通道读取,但可能会返回更具体的错误响应。

  print_r(ftp_raw ,RETR/ path to / some file.csv'\\\\)); 

而我只是说,如果你仍然得到一个文件未找到错误, ;它完全可能的文件真的不存在于假定的位置。在这种情况下,使用手动遍历目录结构, 与 var_dump

或者只需使用(它还支持PASV模式)。它的实现与ext / ftp函数的实现不同。这里有趣的是,URL编码再次是正确的方法,但引用仍然是必要的(不引用自己):

  = file_get_contents(ftp:// user:[email protected]/'path%20to/file%20and.csv'); 
//内联引用可能会跳过一些FTP服务器实现..

但是只是使用。

  //当然你必须使用长时间的PHP curl函数。 
print curl(ftp://.../file with spaces.csv) - > exec();

最后一个选项只是调用Unixland客户端。 (如果不是wget,不是一个简单的ftp客户端。)

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

如果仍然无法检索任何文件,则必须寻找另一个FTP客户端在PHP中进一步调试。猜猜。


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

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.

解决方案

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. Basically the FTP server provides a mini-shell on the command port. As such, typical shell string escaping rules do apply. URL encoding can be ruled out here.

I'd advise first to use single quotes however. Preferrably use escapeshellarg() to apply them. And try ftp_nb_get() while at it.

$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. 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).

Alternatively just use PHPs ftp:// stream wrapper (which also supports PASV mode). Whose implementation is distinct from that of the ext/ftp functions. Here funnily enough, URL encoding is again the correct approach, but quoting still necessary (ftp_fopen_wrapper.c does not quote itself):

= file_get_contents("ftp://user:[email protected]/'path%20to/file%20and.csv'");
// Inline quotes may likely trip up some FTP server implementations..

A much better alternative though is just using 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. (If not wget, than a plain ftp client.)

$url = escapeshellarg("ftp://user:[email protected]/$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. Guess who wrote one.

这篇关于当有“空格”时使用ftp_get()在文件路径和文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:31