I am trying to download a file using PHP from an FTP URL, which looks like ftp://username:password@domain/file.zip
.
The URL is good, because I can download the file pasting it into any browser.
Fopen supports this kind of file streaming, just saw it here: fopen .
Here's my code:
[...]
$destination_folder = '../importmlsupload/';
$url = "ftp://user:password@domain/file.zip";
$newfname = $destination_folder . basename($url);
$file = fopen($url, "rb");
if ($file) {
$newf = fopen($newfname, "wb");
while(!feof($file)) {
$s = fread($file, 1024);
fwrite($newf, $s, 1024 );
}
}
if (!empty($file)) {
fclose($file);
}
if (!empty($newf)) {
fclose($newf);
}
The problem is that I get an ampty file downloaded. I have made some verifications and I got the following results: the file is been created successfully on the server, but the while loop (!feof(...)) ends after the first step. So it reads 1024 bytes and it exits from the loop. By other words feof returns true after the first 1024 bytes or reading.
TRYING WITH HTTP URL RESULTS SUCCESSFUL IMPORT, BUT FTP URL DOESN'T
Am I doing something wrong?