Greetings,

Quick question before I head to bed:

Is it possible to retrieve a file from a remote server via a PHP script?
For example, lets say there's a log file on a remote server that you want to retrieve each day,
sort the log files contents, then dump the info into a MySQL database.

I guess the required steps would be:

1.) Connect to the remote server. Possibly a Windows share.
2.) Suppy the login credentials.
3.) Download the file from the remote server.
4.) Chop-up the file however you wish.
5.) Write info to database.

If it's possible, please point me to the appropriate functions and/or suppy advice on how to make
PHP do this.

Thank you for your time,

Nick

    Connecting to a Windows share is an operating-system level function. You should contact your systems administrator and the sysadmin of the other box as well to determine whether it's possible. If it's important, I'm sure they will put their heads together and figure it out.

    After that, it should be possible to access the files as if they were local by using an appropriate path supplied by your sys admin.

    Mark

      for win-machines:

      try "net use" on the shell

        First off, my system specs:
        -Ubuntu 6 server
        -LAMP installation

        Why doesn't this code work?

        
        <?php
        
        //Connect to a Windows share and download a file.
        
        $ch = curl_init("file://\\\\192.168.1.105\\d$\\test.txt");
        
        	curl_setopt($ch, CURLOPT_USERPWD, "me:pass1");
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        
        	$results = curl_exec($ch);
        	$error = curl_error($ch);
        
        curl_close($ch);
        
        print_r($error);
        print_r($results);
        
        ?>
        
        

        The only piece of documentation I could find about using the curl file:// function is located here:
        http://curl.haxx.se/mail/archive-2001-08/0025.html

        I checked the log files on the Windows server and I do not see any failed logon attempts. I am the adminstrator on both of these computers.

        Thank you for your time,

        Nick

          "\<hostname><share>" isn't valid syntax on a 'nix machine. I don't have much experience with Unix machines, but perhaps you need to use the Samba/NFS service to connect to the Windows share? shrug

            On your Linux box there will be some sysadmin work required to mount the windows share into a locally accessible directory. Talk to your sysadmin (Hint: Make a cifs-filesystem mount at bootup).

            Once it's mounted, just access it using the local directory that your sysadmin mounted it in.

            This all depends on there not being firewalls etc, in between the two boxes which prevent this from working. Talk to your network admin to get these sorted out.

            The UNC path convention used by Windows will definitely not work under Linux.

            Mark

              if you are your admin:

              smbmount //path/to/win/share /path/to/my/local/fs-mountpoint

              you will have to enter some passwords and stuff ...
              ( but you can give usernames/passwords on the command as an option )

                Some updates:

                The following PHP script appears to not fully execute...

                php:
                
                <?php
                
                //Connect to a Windows share and download a file.
                
                    $results = shell_exec("smbclient //192.168.206.1/d$ -U me mypasswd ; get test.txt /home/me/test.txt ; quit");
                
                    print_r($results);
                    echo "<div>$results</div>";
                
                ?>

                ...I checked the Windows logs and the script is connecting then immediately disconnecting and not transferring text.txt. I think "; get test.txt /home/me/test.txt ; quit" is not being executed. Why?

                I can however issue the smbclient commands manually at the command line, one command at a time, and successfully transfer test.txt.

                So next I figured I'd try writing a shell script then executing the shell script in PHP...

                Contents of shell script:

                #!/bin/bash
                #Test script

                smbclient //192.168.206.1/d$ -U me mypasswd && get test.txt /home/me/test.txt && quit

                Contents of php script:

                php:
                
                <?php
                
                //Connect to a Windows share and download a file.
                
                    $results = shell_exec("sh shell");
                
                    print_r($results);
                    echo "<div>$results</div>";
                
                ?>

                ...I checked the Windows logs and the script is connecting then immediately disconnecting and not transferring text.txt. Again, I think "&& get test.txt /home/me/test.txt && quit" is not being executed. Why?

                Thanks,

                Nick

                  The following command executes successfully at a command line:

                  smbclient //192.168.206.1/d$ -U me mypasswd -c 'get test.txt' /home/me/test.txt

                  However the following PHP code fails and returns error "Error opening local file test.txt":

                  PHP code:

                  <?php //Connect to a Windows share and download a file. $results = shell_exec("smbclient //192.168.206.1/d$ -U me mypasswd -c 'get test.txt' /home/me/test.txt"); print_r($results); echo "<div>$results</div>"; ?>

                  Any suggestions? Thanks,

                  Nick

                    The right way to do this is to mount the filesystem (use cifs NOT smb, smb is obsolete) at bootup into a directory somewhere, then use standard PHP file handling to access it.

                    Consult your OS documentation for more info- as you're the sysadmin you should be able to figure it out.

                    smbclient is NOT intended for production use, it only really exists as an internal test program for Samba and to test SMB / CIFS connectivity etc. Do not use it in production.

                    Mark

                      Write a Reply...