Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 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
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*

  • #4
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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; 09-28-2006 at 08:47 PM.

  • #6
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    does the web server have write permission on the file/directory?

  • #8
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*

  • #9
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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

  • #10
    New Coder
    Join Date
    Nov 2005
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •