#1
  1. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    15

    Question NEWBIE:: Using fopen where FTP functions not available


    Hi there.

    I was wondering if anyone can help me. I need to be able to have a php script that will read the last updated time on a file located on a remote FTP server, from where the script is located.

    FTP functions have not been enabled in the version of PHP I have to work with, so I was wondering whether there was any way of doing this with using the fopen commands??

    If someone could help me it would be greatly appreciated.

    Cheers
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Australia
    Posts
    10
    Rep Power
    0
    Of course you _can_ do it.. but is it really worth it..

    Anyway you'll obviously need to emulate an FTP user using fput() & fgets()

    Off the top of my head the commands should go something like:

    USER username
    PASS password
    PORT 127,0,0,1,xyz
    LIST
    -=-=-=-=-=-=-=-=-=-=-=-=-
    PHP Scripts n Stuff
    http://www.bcnstech.net/
    -=-=-=-=-=-=-=-=-=-=-=-=-
  4. #3
  5. No Profile Picture
    Embittered Cynic
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    38.97 N, 94.67 W
    Posts
    11
    Rep Power
    0
    Bummer. I think the answer's no. A few things occur to me though:

    1) if you have curl libraries installed, use that.
    2) you could possibly use fsockopen() and speak raw FTP protocol over the socket. very very ugly. (I wouldn't touch it -- I see nih58 beat me to posting this one ) It's ugly because the FTP data and control connections exist on separate ports, and I'm pretty sure you'd need to manually fsockopen() the data port after parsing the negotiation from the data port....
    +++linear+++
    PHP Security Questions? start here
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Australia
    Posts
    10
    Rep Power
    0
    Yeah.. I think it would be a waste of time trying to talk FTP
    Not to mention you'll have even more fun if for some reason you have to use PASV mode due to connection problems..
    -=-=-=-=-=-=-=-=-=-=-=-=-
    PHP Scripts n Stuff
    http://www.bcnstech.net/
    -=-=-=-=-=-=-=-=-=-=-=-=-
  8. #5
  9. Big Endian
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    May 2001
    Location
    Fly-over country
    Posts
    1,172
    Rep Power
    30
    I have no idea if this will work or not. I'm just throwing it out as a hack that I used once.

    I had to run an FTP command from a VB program to an IBM mainframe. The FTP command would always work from the command prompt, but would fail using FTP controls and .dll's in my program.

    Eventually I had to put all of the FTP commands into a text file. This text file had the login, password, command information, etc. I tweaked the flat file until I could run the command ftp -s:filename successfully from the system prompt.

    When this ran successfully, I then ran it from inside my program using a shell command (in PHP you would want to use the system command). If you were able to pipe the output from the above command into a file on your local machine, you would be able to use fopen to examine the contents of this file for the information you needed.

    As in my case, it wouldn't matter if your programming language had FTP support as long as you were able to run the command manually from the command prompt. You would have to run a command something like this:

    ftp -s:commandfile > outputfile

    Then use fopen to examine the contents of outputfile.

    If you get really desperate, you might want to give it a try...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Australia
    Posts
    10
    Rep Power
    0
    you would want to use the system command).
    if you use exec("$command_line",$results);

    then the results of $command_line being executed would be stored in an array.. obviously $results
    -=-=-=-=-=-=-=-=-=-=-=-=-
    PHP Scripts n Stuff
    http://www.bcnstech.net/
    -=-=-=-=-=-=-=-=-=-=-=-=-
  12. #7
  13. No Profile Picture
    Embittered Cynic
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    38.97 N, 94.67 W
    Posts
    11
    Rep Power
    0
    The ftp -s hack is a Windoze idiom. Most *nix ftp clients will let you cut to the chase thusly (consult your manpage):

    ftp ftp://[user:password@]host[:port]/file[/]
    ftp http://host[:port]/file
    ftp host:[/path/]file[/]

    and will even expand globs.
    +++linear+++
    PHP Security Questions? start here
  14. #8
  15. No one Important
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Australia
    Posts
    524
    Rep Power
    15
    Thanks for all the replies...

    But what I am after this for is to have a page that shows the clients when a file was last updated on our ftp server so they know how current the download file is.

    The version of PHP that I am using was not configured with ftp functions and I am unable to change this.

    So I was wondering if there was a way with the fopen to get the last update time of the file from the remote ftp server......

    Does anyone know how I might do this?
  16. #9
  17. No Profile Picture
    Embittered Cynic
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    38.97 N, 94.67 W
    Posts
    11
    Rep Power
    0
    fopen won't do that.

    Hold on, you said it's *your* ftp server?? That changes things. There are a number of ways you can work around the ftp limitation if you have access to the file is sits on.

    If it were me, I'd write a shell script that ran ls -l, pick a port to run it and throw an entry into inetd.conf (essentially using inetd as a daemon to listen for requests on your selected port, and running the ls shell script when it gets a request). This is reasonably secure too (since your script doesn't accept input, it's hard to exploit). Then all you need to do is use fsockopen() to talk to your little daemon and munge the results in PHP.

    You could alternatively nfs mount the directories of interest (ugh--don't do this). You could run a httpd and use HEAD requests to get the file modification time, and restrict the httpd so it only talks to your PHP web server. There's a number of other variations on the theme, but if you own the server of interest, lots of possibilities exist.
    Last edited by linear; July 20th, 2001 at 12:24 AM.
    +++linear+++
    PHP Security Questions? start here
  18. #10
  19. No Profile Picture
    Embittered Cynic
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    38.97 N, 94.67 W
    Posts
    11
    Rep Power
    0
    ~puts *nix weenie hat on~

    Three steps and this is done.

    1) write this trivial shell script
    Code:
    #!/bin/sh
    ls -l /path/to/the/directory
    and install it somewhere you'll remember (/usr/libexec/mystik.sh is good). Set it executable.

    2) edit /etc/services (as root) and add the line
    mystik 30003/tcp #mystik's disk daemon

    3) edit /etc/inetd.conf and add the line
    mystik stream tcp root /usr/libexec/mystik.sh mystik.sh
    and restart inetd (kill -HUP `cat /var/run/inetd.pid`)

    You can telnet to port 30003 on the ftp serve, and if you did the steps right, the output of ls -l should get barfed out at you.

    Isn't *nix great??!?
    Last edited by linear; July 20th, 2001 at 12:38 AM.
    +++linear+++
    PHP Security Questions? start here
  20. #11
  21. No Profile Picture
    Embittered Cynic
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    38.97 N, 94.67 W
    Posts
    11
    Rep Power
    0
    bump
    +++linear+++
    PHP Security Questions? start here

IMN logo majestic logo threadwatch logo seochat tools logo