[thelist] PHP FTP download Snippet

Anthony Baratta Anthony at Baratta.com
Sat Aug 31 23:17:01 CDT 2002


At 04:29 PM 8/31/2002, CDitty wrote:
>Hello all,
>
>Hope everyone has an excellent holiday weekend.  I am writing a small php
>script that connects via ftp to a site and will download a certain file.  I
>have figured out the connection part, but I cannot get the download section
>to work like I need.  Can anyone look at my code and see what's
>wrong?  Currently, my file is filled with "1"s.

Chris....

I'm trying to read your code and I'm not understanding what you are trying
to do. If all you are attempting to do is download, via FTP a specific
file, I think this code is over designed.

>         $ret = ftp_get($fp, $filename, $filename, FTP_ASCII);

This should not return until done, since it is not an async function.
Therefore this next line is not needed.

>         while ($ret == FTP_MOREDATA) {

And I am confused here as to why you are attempting to open the local file
for writing before the download is completed, if you beleived that the
ftp_get was async.

>         // Do whatever you want
>         echo ".";
>
>                 $fpWrite = fopen("$filename", "w");
>                 while(!feof($fpWrite)){
>                         $buffer=$ret;
>                         fputs($fpWrite,$buffer);
>                 }
>
>                 fclose($fpWrite);

Also why are you attempting to reconnected (async) to continue the
download, if it's not finished yet - hence the do while loop this is in.

>         // Continue downloading...
>         $ret = ftp_async_continue ($my_connection);
>         }



>         if ($ret != FTP_FINISHED) {
>                  echo "There was an error downloading the file...";
>         }

Lastly, both ftp_get and ftp_async_continue return booleans not strings, so
unless FTP_FINISHED is a boolean constant, I don't think that the above
line will validate correctly for you.

I'm not the PHP guru, but I think that is the proper reading of your code
snippet.
---
Anthony Baratta
President
Keyboard Jockeys

"Conformity is the refuge of the unimaginative."




More information about the thelist mailing list