...

View Full Version : Connect to network share



nkline
09-25-2006, 11:51 PM
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*

Mwnciau
09-26-2006, 12:25 AM
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 (http://uk2.php.net/manual/en/ref.ftp.php) are the PHP FTP functions.

nkline
09-27-2006, 03:18 AM
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.

Thank you for your time,

*Nick*

nkline
09-28-2006, 08:21 PM
Some updates:

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


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

//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*

GJay
09-28-2006, 08:34 PM
try:


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

nkline
09-28-2006, 10:28 PM
I'm getting closer. Thank you for your reply. 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*

GJay
09-28-2006, 10:31 PM
does the web server have write permission on the file/directory?

nkline
09-28-2006, 10:50 PM
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*

GJay
09-28-2006, 11:25 PM
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

nkline
09-29-2006, 01:04 AM
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.

GJay
09-29-2006, 08:40 AM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum