From: m dot ebbers at i-real dot nl
Operating system: Linux
PHP version: 5.3.1
PHP Bug Type: Streams related
Bug description: Using a ftp stream to a windows ftp server to upload results
in missing data
Description:
When fopen/fwrite are used to upload a file, through ftp to a ftp server
running on windows, it is not always uploaded completely despite the fact
that fwrite returns that all bytes of the file are written.
I've testen the following scenarios with the attached code:
From Ubuntu 9.10 to Bulletproof ftpd under windows xp (vmware). (failed)
From Ubuntu 9.10 to Serv-u ftpd under windows xp (vmware). (failed)
From Ubuntu 9.10 to vsftpd on same machine. (ok)
Different hardware and network:
From CentOS release 5 to Bulletproof ftpd on windows server (failed)
When using the ftp command it all works great.
Also tried the build-in ftp client from php and that works fine. It only
failed when using fopen/fwrite/file_put_contents.
Reproduce code:
---
$host = '192.168.1.34';
$user = 'marke';
$passwd = 'ebbers';
$path = '/';
$file = $argv[1];
$url='ftp://'.$user.':'.$passwd.'@'.$host.$path.$file;
$content = file_get_contents($file);
$handle = fopen($url, 'w');
$written = 0;
while ($written != strlen($content))
{
$write = fwrite($handle, substr($content, $written));
fflush($handle);
if($write){
$written .= $write;
echo Written: .$written.'\n';
}else{
break;
}
}
Expected result:
Output script: Written: 293346 (Test file is 293346 bytes.)
And a file on the ftp server of the same size.
Actual result:
--
Output script: Written: 293346 (Test file is 293346 bytes.)
A file on the server, but it is smaller. (and the sizes varies)
I've also a wireshark sniff available. The strange thing in the sniff is
that the every byte of the file is actually send, but by an unknown reason
there is tcp resend and the data in that resend is also the last data in
the file on the server.
Strace:
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 3
socket(PF_NETLINK, SOCK_RAW, 0) = 3
bind(3, {sa_family=AF_NETLINK, pid=0, groups=}, 12) = 0
getsockname(3, {sa_family=AF_NETLINK, pid=6499, groups=}, [12]) =
0
sendto(3, \24\0\0\0\26\0\1\3\220\321oK\0\0\0\0\0\0\0\0, 20, 0,
{sa_family=AF_NETLINK, pid=0, groups=}, 12) = 20
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=},
msg_iov(1)=[{0\0\0\0\24\0\2\0\220\321oKc\31\0\0\2\10\200\376\1\0\0\0\10\0\1\0\177\0\0\1...,
4096}], msg_controllen=0, msg_flags=0}, 0) = 228
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=},
msg_iov(1)=[{@\0\0\0\24\0\2\0\220\321oKc\31\0\0\n\200\200\376\1\0\0\0\24\0\1\0\0\0\0\0...,
4096}], msg_controllen=0, msg_flags=0}, 0) = 256
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=},
msg_iov(1)=[{\24\0\0\0\3\0\2\0\220\321oKc\31\0\0\0\0\0\0\1\0\0\0\24\0\1\0\0\0\0\0...,
4096}], msg_controllen=0, msg_flags=0}, 0) = 20
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
connect(3, {sa_family=AF_INET, sin_port=htons(21),
sin_addr=inet_addr(192.168.1.34)}, 16) = -1 EINPROGRESS (Operation now in
progress)
getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
recv(3, 220 Serv-U FTP Server v9.3 ready..., 8192, MSG_DONTWAIT) = 37
send(3, USER marke\r\n, 12, MSG_DONTWAIT) = 12
recv(3, 331 User name okay, need passwor..., 8192, MSG_DONTWAIT) = 36
send(3, PASS ebbers\r\n, 13, MSG_DONTWAIT) = 13
recv(3, 230 User logged in, proceed.\r\n, 8192, MSG_DONTWAIT) = 30
send(3, TYPE I\r\n, 8, MSG_DONTWAIT) = 8
recv(3, 200 Type set to I.\r\n, 8192, MSG_DONTWAIT) = 20
send(3, SIZE /bin2\r\n, 12, MSG_DONTWAIT) = 12
recv(3, 550 /bin2: No such file.\r\n, 8192, MSG_DONTWAIT) = 26
send(3, EPSV\r\n, 6, MSG_DONTWAIT)= 6
recv(3, 500 'EPSV': command not understo..., 8192, MSG_DONTWAIT) = 37
send(3, PASV\r\n, 6, MSG_DONTWAIT)= 6
recv(3, 227 Entering Passive Mode (192,1..., 8192, MSG_DONTWAIT) = 47
send(3, STOR /bin2\r\n, 12, MSG_DONTWAIT) = 12
socket(PF_NETLINK, SOCK_RAW, 0) = 4
bind(4, {sa_family=AF_NETLINK, pid=0, groups=}, 12) = 0
getsockname(4, {sa_family=AF_NETLINK, pid=6499, groups=}, [12]) =
0
sendto(4, \24\0\0\0\26\0\1\3\220\321oK\0\0\0\0\0\0\0\0, 20, 0,
{sa_family=AF_NETLINK, pid=0, groups=}, 12) = 20
recvmsg(4, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=},
msg_iov(1)=[{0\0\0\0\24\0\2\0\220\321oKc\31\0\0\2\10\200\376\1\0\0\0\10\0\1\0\177\0\0\1...,
4096}], msg_controllen=0, msg_flags=0}, 0) = 228
recvmsg(4, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=},
msg_iov(1)=[{@\0\0\0\24\0\2\0\220\321oKc\31\0\0\n\200\200\376\1\0\0\0\24\0\1\0\0\0\0\0...,
4096}], msg_controllen=0, msg_flags=0}, 0) = 256
recvmsg(4, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=},
msg_iov(1)=[{\24\0\0\0\3\0\2\0\220\321oKc\31\0\0\0\0\0\0\1\0\0\0\24\0\1\0\0\0\0\0...,
4096}], msg_controllen=0, msg_flags