Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grabbing files from ftp site

Status
Not open for further replies.

hinchdog

Programmer
Feb 14, 2001
380
US
can a php script grab and download files from an outsite ftp site and store them on the server? if so, could you show me some code to do this? thanks
 
yes it can. A piece of code, just go to and look for the documentation of ftp functions. It's the best thing to do. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i don't think ftp has been enabled on my host. i get a "function not defined" when trying to use the ftp_connect method. is there a way around this?
 
well, you must enable ftp support

If you are using windows, you must edit php.ini and uncomment the extension=php_ftp.dll (i think is this name)

if you are using linux you must recompile php with ftp support (--with-ftp) i think Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i don't think my webhost will allow that. but thanks anyway
 
It is still possible to do get the file using an external ftp command.

Here's what works from my Linux box:

I've created a file called ftpcommands that reads like this:
[tt]open <IP-address or ftp site name>
user <mylogin> <my password>
lcd <directory where the web server can write>
cd <directory where the file sits on foreign FTP server>
bin
get <file I need to get>
quit
[/tt]

The run the following PHP script:

[tt]<?php
exec (&quot;cat ftpcommands | ftp -n&quot;, $foo);
?>
[/tt]

[Notice the command on Linux is &quot;ftp -n&quot;. That suppresses autologin, which allows the USER line of the ftpcommands file to work.]

If you're on Win32, the similar thing can work, with the following changes:

ftpcommands:
[tt]open <ftp server>
<mylogin>
<mypassword>
lcd <writeable local directory>
cd <directory for file>
bin
get <filename>
quit[/tt]

And the exec() statement should read:

[tt]exec (&quot;ftp -s:<full path and name to ftpcommands>&quot;);[/tt] ______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
sleipnir214,

That's a very imaginative solution. I'm wondering if there might be a similar approach when it comes to hosts with disabled socket connections? Some free PHP hosts prevent users from &quot;initiating&quot; any kind of outgoing socket connection. Thanks for your reply.
 
He didn't specify the size of the file. fopen(), etc, won't be the best solution if the file were sufficiently large. ______________________________________________________________________
If this post was helpful,
click the link below
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top