Web Analytics Made Easy -
StatCounter Connect to network share - CodingForum

Announcement

Collapse
No announcement yet.

Connect to network share

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connect to network share

    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*

  • #2
    If the file is a file which is accessable on the internet you can just use the file() function to get the contents, if it's not the only other solution I can think of is using ftp, here are the PHP FTP functions.

    Comment


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

      Why doesn't this code work?

      PHP Code:

      <?php

      //Connect to a Windows share and download a file.

          
      $ch curl_init("file://\\\\192.168.1.105\\d$\\test.txt");

              
      curl_setopt($chCURLOPT_USERPWD"me:pass1");
              
      curl_setopt($chCURLOPT_RETURNTRANSFER1);
              
      curl_setopt($chCURLOPT_CONNECTTIMEOUT5);

              
      $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.

      Thank you for your time,

      *Nick*

      Comment


      • #4
        Some updates:

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

        Code:
        <?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:

        Code:
        #!/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:

        Code:
        <?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*

        Comment


        • #5
          try:
          PHP Code:
          smbclient //192.168.206.1/d$ -U me mypasswd -c 'get test.txt' 
          using -c (command string) to pass in the command to the function
          Last edited by Spookster; Sep 28, 2006, 02:47 PM.
          My thoughts on some things: http://codemeetsmusic.com
          And my scrapbook of cool things: http://gjones.tumblr.com

          Comment


          • #6
            I'm getting closer. Thank you for your reply. The following command executes successfully at a command line:

            Code:
            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:
            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*

            Comment


            • #7
              does the web server have write permission on the file/directory?
              My thoughts on some things: http://codemeetsmusic.com
              And my scrapbook of cool things: http://gjones.tumblr.com

              Comment


              • #8
                I don't believe Apache has write permission to my /home/username directory, since I haven't changed any permissions since I installed LAMP. So it looks like I would have to grant these permissions to Apache? How might I go about doing that?

                Another idea I had, is it possible to just write the contents of test.txt to a string and then have PHP deal with it from there?

                Thanks,

                *Nick*

                Comment


                • #9
                  while you're still trying to get things working, the easiest way is to use:
                  chmod 777 /home/you

                  doing this isn't the most sensible thing in the world, you'd be better off keeping things somewhere other than your home directory (either a web-root, or /tmp or something) and setting the group-owner to be that of the webserver:
                  chown you:www-data /path/to/file.txt
                  My thoughts on some things: http://codemeetsmusic.com
                  And my scrapbook of cool things: http://gjones.tumblr.com

                  Comment


                  • #10
                    Unreal, I changed the permissions for /home/me/test.txt to:

                    -rwxrwxrwx www-data me test.txt

                    and the PHP script still doesn't work. It's reporting the same error as before. So, I then changed permissions to:

                    -rwxrwxrwx www-data www-data test.txt

                    and still no success! Now I'm definitely confused.

                    Comment


                    • #11
                      with close to zero knowledge of smbclient (I know basically what it is...), what happens if you use different file names for the source and target? (make one of them test2.txt instead). It makes sense that the file where the script is running would be the 'local' file, but just to rule out some possibilities.
                      What happens if you give an absolute path to the 'get'?
                      I assume that the smb setup means that when you connect, you're in a directory by default, are files in this location readable by the necessary users? (i.e. everyone would be a good start)
                      My thoughts on some things: http://codemeetsmusic.com
                      And my scrapbook of cool things: http://gjones.tumblr.com

                      Comment

                      Working...
                      X