php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20526 fopen with ftp URL returns false despite ftp server executes get
Submitted: 2002-11-20 18:01 UTC Modified: 2002-12-06 12:06 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ginsinga at webmail dot laben dot it Assigned:
Status: Not a bug Package: FTP related
PHP Version: 4.3.0RC1 OS: MS Windows XP Pro
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ginsinga at webmail dot laben dot it
New email:
PHP Version: OS:

 

 [2002-11-20 18:01 UTC] ginsinga at webmail dot laben dot it
fopen with ftp URL returns false despite ftp server executes get

Tested in loopback with IP 127.0.0.1 and BulletProof ftp server running on the same machine.
Note, in the server log pasted here below, that the ftp client seems to be quitting the
connection before the transfer is complete.

Used PHP version: php-4.3.0RC1-Win32.zip
OS.: Microsoft Windows XP Professional 2002

php.ini was a copy of php.ini-recommended with only
following modif.:
extension_dir = C:\PHP
cgi.force_redirect = 0 

============  My PHP program ======================

<?php

$FTPStr = ftp_connect('127.0.0.1');
ftp_login($FTPStr,'anonymous','');
$FileNames = ftp_nlist($FTPStr,'pub/*.txt');
ftp_quit($FTPStr);
foreach ($FileNames as $Current) {
  printf("%s\n",$Current);
}

if (!($File = fopen ('ftp://anonymous:@127.0.0.1/pub/short.txt','r'))) {
  printf("Cant't open file\n");
  return;
}
$Count = 1;
while (!feof ($File)) {
  $line = fgets ($File, 1024);
  printf("%d %s\n",$Count,$line);
  $Count += 1;
}
fclose($File);

?>

============ Running the program

C:\PHP>php -f c:\inetpub\wwwroot\ftp_ls_and_wrap_fopen.php
PHP Warning:  fopen() [http://www.php.net/function.fopen]: php_hostconnect: conn
ect failed in c:\inetpub\wwwroot\ftp_ls_and_wrap_fopen.php on line 11
PHP Warning:  fopen(ftp://...@127.0.0.1/pub/short.txt) [http://www.php.net/funct
ion.fopen]: failed to create stream: FTP server reports 227 Entering Passive Mod
e (127.0.0.1 in c:\inetpub\wwwroot\ftp_ls_and_wrap_fopen.php on line 11
dummy1.txt
dummy2.txt
short.txt
Cant't open file

================ (Pasted) log from BulletProof FTP Server v2.15 ========================

21/11/2002 00:33:59 - FTP Server On-line : IP(s) 192.168.1.34, on port 21
(000001) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > connected to ip : 127.0.0.1
(000001) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > sending welcome message.
(000001) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > 220 BulletProof FTP Server ready ...
(000001) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > USER anonymous
(000001) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > 331 Password required for anonymous.
(000001) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > PASS ********
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > logged in.
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 230 User anonymous logged in.
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > PORT 127,0,0,1,10,77
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 200 Port command successful.
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > NLST pub/*.txt
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 150 Opening data connection for directory list.
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 226 Transfer ok
(000001) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > disconnected. (00:00:00)
(000002) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > connected to ip : 127.0.0.1
(000002) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > sending welcome message.
(000002) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > 220 BulletProof FTP Server ready ...
(000002) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > USER anonymous
(000002) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > 331 Password required for anonymous.
(000002) 21/11/2002 00:34:07 - (not logged in) (127.0.0.1) > PASS ********
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > logged in.
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 230 User anonymous logged in.
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > TYPE I
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 200 Type set to I.
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > SIZE /pub/short.txt
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 213 24
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > EPSV 
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 500 Unknown command.
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > PASV 
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 227 Entering Passive Mode (127,0,0,1,82,146).
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > RETR /pub/short.txt
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > asked to download 'E:\FTPdir\pub\short.txt' --> 

Access allowed.
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > QUIT 
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 221 Bye bye ...
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > finished downloading 'E:\FTPdir\pub\short.txt' -  

(00:34:08 - 0.000 KB - 0.000 KBytes/s)
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > 226 Transfer ok
(000002) 21/11/2002 00:34:07 - anonymous (127.0.0.1) > disconnected. (00:00:00)


==================== with the basic ftp client of Windows, "get" works and file is not empty...

C:\PHP>ftp 127.0.0.1
Connected to 127.0.0.1.
220 BulletProof FTP Server ready ...
User (127.0.0.1:(none)): anonymous
331 Password required for anonymous.
Password:
230 User anonymous logged in.
ftp> ls /pub/*.txt
200 Port command successful.
150 Opening data connection for directory list.
dummy1.txt
dummy2.txt
short.txt
226 Transfer ok
ftp: 35 bytes received in 0.00Seconds 35000.00Kbytes/sec.
ftp> get /pub/short.txt
200 Port command successful.
150 Opening data connection for short.txt (24 bytes).
226 Transfer ok
ftp: 24 bytes received in 0.00Seconds 24000.00Kbytes/sec.
ftp> disconnect
221 Bye bye ...
ftp> bye

C:\PHP>type short.txt
Pippo
Pluto
Paperino

C:\PHP>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-24 12:53 UTC] iliaa@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-latest.zip

Cannot replicate the problem on WinXP or Linux with a variety of FTP servers.
 [2002-11-25 05:41 UTC] ginsinga at webmail dot laben dot it
Tried http://snaps.php.net/win32/php4-win32-latest.zip with same result.

One of my reasons to suspect it is a bug is that the problem reported by PHP (FTP server reports 227), in my opinion, should not indicate an error at all.
I am now trying to arrange a debug session by myself (I just downloaded Cygwin, PHP sources and other stuff suggested in the PHP manual, but this is for me quite a challenge, since I got somewhat rusted with software development :-)
Wish me good luck...
Ciao.

Giovanni
 [2002-11-25 16:19 UTC] iliaa@php.net
Could you please try the latest snapshot that will be avaliable from http://snaps.php.net in a few hours. It contains a fix that will make the code return a proper error message.
 [2002-11-26 05:56 UTC] ginsinga at webmail dot laben dot it
In the mean time I realized that the "FTP reports 227.." is not to be intended as "the error"; "the error" is "...failed to create stream..."; "FTP reports 227" is the result of a mere diagnostic aid.

In addition I can see now that the problem is time-dependent; sometimes is "...failed to create stream...", sometimes the fopen returns a stream with no data (while there should be some!).

Running a debug endowed rebuild of php-4.3.0RC1-Win32.zip and tracing the execution of ftp_fopen_wrapper.c I have seen that the "goto errexit" in line 419 is executed (datastream == NULL); it is strange to me that the datastream opening is attempted *after* that the QUIT command has been sent via the FTP control stream. Is that normal? I suspect that the FTP server gets the QUIT and kills or inhibits the data connection.
I will try to momentarily comment-out the QUIT and see what happens.

Anyway I will also look at and try the snapshot as you suggest.

bye for now

Giovanni
 [2002-11-26 17:42 UTC] ginsinga at webmail dot laben dot it
Tried to comment-out lines 407 to 411 of file ftp_fopen_wrapper.c.
With this temporary patch my little PHP program successfully reads the file via the wrapper!!
Of course in no way this can be considered "a solution".
In practice I have stripped off the QUIT.
The question: should not the QUIT be sent only at fclose?
 [2002-11-27 16:18 UTC] ginsinga at webmail dot laben dot it
Tried php4-win32-STABLE-200211271730.
Tried also php-4.3.0RC2-Win32.

Unfortunately the behaviour of fopen with stp URL, in my opinion, is still not correct.
Now the connection on the FTP data channel is established before issuing the QUIT command on the control channel (this was not so with previous versions); anyway the QUIT command is issued without waiting the end of the data transfer; depending on the time, the QUIT can reach the FTP server AFTER or BEFORE that the transfer is completed.
Testing in loopback, most of the times the transfer is successful with the Bulletproof FTP server (http://www.bpftpserver.com/), but always fails with the GuildFTPd server (http://www.guildftpd.com/).

In my opinion issuing of QUIT should be postponed until the end of the data transfer (or perhaps until fclose?).

Many thanks for your attention.

Giovanni.
 [2002-12-06 09:53 UTC] iliaa@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

FTP protocol uses 2 ports 1 for sending commands and the other for sending data. The connection that is being closed is the 'command' port since all the necessary commands have been already sent. The data port remains open until the file is sent/recieved at which point it is also closed.
If the FTP server does not support this functionality I suspect it is the problem with the FTP server. Non of the FTP servers I've tried ncftpd, proftpd, wu-ftpd,  BulletProof ftp and severals others have experienced this problem.
 [2002-12-06 12:06 UTC] ginsinga at webmail dot laben dot it
OK. One last line from my side, then I will quit.
I never intended to ask support questions here; I was (and in part I still am) of the opinion it was a real bug, otherwise I had spontaneously applied to other appropriate resources.
It looks like your last comment in this page is actually a reply to my private e-mail.
Regards.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC